• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

liip / LiipImagineBundle / 10701363663

04 Sep 2024 12:11PM UTC coverage: 81.688% (-0.06%) from 81.746%
10701363663

push

github

web-flow
Merge pull request #1610 from liip/cs-fix

apply latest cs fixer

67 of 84 new or added lines in 42 files covered. (79.76%)

2 existing lines in 1 file now uncovered.

2275 of 2785 relevant lines covered (81.69%)

103.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
/Imagine/Filter/PostProcessor/AbstractPostProcessor.php
1
<?php
2

3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11

12
namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor;
13

14
use Liip\ImagineBundle\Binary\BinaryInterface;
15
use Liip\ImagineBundle\Binary\FileBinaryInterface;
16
use Symfony\Component\Filesystem\Exception\IOException;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\Process\Process;
19

20
abstract class AbstractPostProcessor implements PostProcessorInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $executablePath;
26

27
    /**
28
     * @var string|null
29
     */
30
    protected $temporaryRootPath;
31

32
    /**
33
     * @var Filesystem
34
     */
35
    private $filesystem;
36

37
    public function __construct(string $executablePath, ?string $temporaryRootPath = null)
38
    {
39
        $this->executablePath = $executablePath;
2,587✔
40
        $this->temporaryRootPath = $temporaryRootPath;
2,587✔
41
        $this->filesystem = new Filesystem();
2,587✔
42
    }
1,393✔
43

44
    protected function createProcess(array $arguments = [], array $options = []): Process
45
    {
46
        $process = new Process($arguments);
1,053✔
47

48
        if (!isset($options['process'])) {
1,053✔
49
            return $process;
1,040✔
50
        }
51

52
        if (isset($options['process']['timeout'])) {
13✔
53
            $process->setTimeout($options['process']['timeout']);
13✔
54
        }
55

56
        if (isset($options['process']['working_directory'])) {
13✔
57
            $process->setWorkingDirectory($options['process']['working_directory']);
13✔
58
        }
59

60
        if (isset($options['process']['environment_variables']) && \is_array($options['process']['environment_variables'])) {
13✔
61
            $process->setEnv($options['process']['environment_variables']);
13✔
62
        }
63

64
        return $process;
13✔
65
    }
66

67
    protected function isBinaryTypeJpgImage(BinaryInterface $binary): bool
68
    {
69
        return $this->isBinaryTypeMatch($binary, ['image/jpeg', 'image/jpg']);
351✔
70
    }
71

72
    protected function isBinaryTypePngImage(BinaryInterface $binary): bool
73
    {
74
        return $this->isBinaryTypeMatch($binary, ['image/png']);
559✔
75
    }
76

77
    protected function isBinaryTypeMatch(BinaryInterface $binary, array $types): bool
78
    {
79
        return \in_array($binary->getMimeType(), $types, true);
1,118✔
80
    }
81

82
    protected function writeTemporaryFile(BinaryInterface $binary, array $options = [], ?string $prefix = null): string
83
    {
84
        $temporary = $this->acquireTemporaryFilePath($options, $prefix);
988✔
85

86
        if ($binary instanceof FileBinaryInterface) {
988✔
87
            $this->filesystem->copy($binary->getPath(), $temporary, true);
676✔
88
        } else {
89
            $this->filesystem->dumpFile($temporary, $binary->getContent());
676✔
90
        }
91

92
        return $temporary;
988✔
93
    }
94

95
    protected function acquireTemporaryFilePath(array $options, ?string $prefix = null): string
96
    {
97
        $root = $options['temp_dir'] ?? $this->temporaryRootPath ?: sys_get_temp_dir();
988✔
98

99
        if (!is_dir($root)) {
988✔
100
            try {
101
                $this->filesystem->mkdir($root);
91✔
102
            } catch (IOException $exception) {
×
103
                // ignore failure as "tempnam" function will revert back to system default tmp path as last resort
104
            }
105
        }
106

107
        if (false === $file = @tempnam($root, $prefix ?: 'post-processor')) {
988✔
NEW
108
            throw new \RuntimeException(\sprintf('Temporary file cannot be created in "%s"', $root));
×
109
        }
110

111
        return $file;
988✔
112
    }
113

114
    /**
115
     * @param int[]    $validReturns
116
     * @param string[] $errors
117
     */
118
    protected function isSuccessfulProcess(Process $process, array $validReturns = [0], array $errors = ['ERROR']): bool
119
    {
120
        if (\count($validReturns) > 0 && !\in_array($process->getExitCode(), $validReturns, true)) {
1,157✔
121
            return false;
546✔
122
        }
123

124
        foreach ($errors as $string) {
611✔
125
            if (false !== mb_strpos($process->getOutput(), $string)) {
442✔
126
                return false;
26✔
127
            }
128
        }
129

130
        return true;
585✔
131
    }
132

133
    protected function triggerSetterMethodDeprecation(string $method): void
134
    {
135
        @trigger_error(\sprintf('The %s() method was deprecated in 2.2 and will be removed in 3.0. You must '
65✔
136
            .'setup the class state via its __construct() method. You can still pass filter-specific options to the '.
30✔
137
            'process() method to overwrite behavior.', $method), E_USER_DEPRECATED);
65✔
138
    }
35✔
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc