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

68publishers / image-storage / 22296382066

23 Feb 2026 07:14AM UTC coverage: 87.603% (-3.8%) from 91.356%
22296382066

Pull #61

github

web-flow
Merge 9d5a8b32a into 57d73edfb
Pull Request #61: Better Presets

148 of 227 new or added lines in 17 files covered. (65.2%)

4 existing lines in 1 file now uncovered.

1484 of 1694 relevant lines covered (87.6%)

0.88 hits per line

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

85.0
/src/Modifier/Facade/ModifierFacade.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\ImageStorage\Modifier\Facade;
6

7
use Intervention\Image\Image;
8
use SixtyEightPublishers\FileStorage\Config\ConfigInterface;
9
use SixtyEightPublishers\FileStorage\PathInfoInterface;
10
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
11
use SixtyEightPublishers\ImageStorage\Modifier\Applicator\ModifierApplicatorInterface;
12
use SixtyEightPublishers\ImageStorage\Modifier\Codec\CodecInterface;
13
use SixtyEightPublishers\ImageStorage\Modifier\Collection\ModifierCollectionInterface;
14
use SixtyEightPublishers\ImageStorage\Modifier\ModifierInterface;
15
use SixtyEightPublishers\ImageStorage\Modifier\Preset\PresetCollectionInterface;
16
use SixtyEightPublishers\ImageStorage\Modifier\Validator\ValidatorInterface;
17
use function is_array;
18
use function is_string;
19
use function sprintf;
20

21
final class ModifierFacade implements ModifierFacadeInterface
22
{
23
    /** @var array<ModifierApplicatorInterface> */
24
    private array $applicators = [];
25

26
    /** @var array<ValidatorInterface> */
27
    private array $validators = [];
28

29
    public function __construct(
1✔
30
        private readonly ConfigInterface $config,
31
        private readonly CodecInterface $codec,
32
        private readonly PresetCollectionInterface $presetCollection,
33
        private readonly ModifierCollectionInterface $modifierCollection,
34
    ) {}
1✔
35

36
    public function setModifiers(array $modifiers): void
37
    {
38
        foreach ($modifiers as $modifier) {
1✔
39
            if (!$modifier instanceof ModifierInterface) {
1✔
40
                throw new InvalidArgumentException(sprintf(
1✔
41
                    'The argument passed into the method %s() must be an array of %s.',
1✔
42
                    __METHOD__,
1✔
43
                    ModifierInterface::class,
1✔
44
                ));
45
            }
46

47
            $this->modifierCollection->add($modifier);
1✔
48
        }
49
    }
1✔
50

51
    public function setPresets(array $presets): void
52
    {
53
        foreach ($presets as $name => $preset) {
1✔
54
            $this->presetCollection->add((string) $name, $preset);
1✔
55
        }
56
    }
1✔
57

58
    public function setApplicators(array $applicators): void
59
    {
60
        foreach ($applicators as $applicator) {
1✔
61
            if (!$applicator instanceof ModifierApplicatorInterface) {
1✔
62
                throw new InvalidArgumentException(sprintf(
1✔
63
                    'The argument passed into the method %s() must be an array of %s.',
1✔
64
                    __METHOD__,
1✔
65
                    ModifierApplicatorInterface::class,
1✔
66
                ));
67
            }
68

69
            $this->applicators[] = $applicator;
1✔
70
        }
71
    }
1✔
72

73
    public function setValidators(array $validators): void
74
    {
75
        foreach ($validators as $validator) {
1✔
76
            if (!$validator instanceof ValidatorInterface) {
1✔
77
                throw new InvalidArgumentException(sprintf(
1✔
78
                    'The argument passed into the method %s() must be an array of %s.',
1✔
79
                    __METHOD__,
1✔
80
                    ValidatorInterface::class,
1✔
81
                ));
82
            }
83

84
            $this->validators[] = $validator;
1✔
85
        }
86
    }
1✔
87

88
    public function getModifierCollection(): ModifierCollectionInterface
89
    {
90
        return $this->modifierCollection;
1✔
91
    }
92

93
    public function getPresetCollection(): PresetCollectionInterface
94
    {
NEW
95
        return $this->presetCollection;
×
96
    }
97

98
    public function getCodec(): CodecInterface
99
    {
100
        return $this->codec;
1✔
101
    }
102

103
    public function modifyImage(Image $image, PathInfoInterface $info, string|array $modifiers, bool $stripMeta = false): ModifyResult
104
    {
105
        if (is_string($modifiers)) {
1✔
106
            $codec = $this->getCodec();
1✔
107
            $modifiers = $codec->expandModifiers(value: $modifiers);
1✔
108
        }
109

110
        if (empty($modifiers)) {
1✔
111
            throw new InvalidArgumentException('Unable to modify the image, modifiers are empty.');
1✔
112
        }
113

114
        $values = $this->modifierCollection->parseValues($modifiers);
1✔
115

116
        if ($stripMeta) {
1✔
UNCOV
117
            $values->add('__stripMeta', true);
×
118
        }
119

120
        foreach ($this->validators as $validator) {
1✔
121
            $validator->validate($values, $this->config);
1✔
122
        }
123

124
        $modified = false;
1✔
125
        $encodeFormat = null;
1✔
126
        $encodeQuality = null;
1✔
127

128
        foreach ($this->applicators as $applicator) {
1✔
129
            foreach ($applicator->apply($image, $info, $values, $this->config) as $key => $value) {
1✔
130
                if (ModifierApplicatorInterface::OutImage === $key && $value instanceof Image) {
1✔
131
                    $image = $value;
1✔
132
                    $modified = true;
1✔
133

134
                    continue;
1✔
135
                }
136

137
                if (ModifierApplicatorInterface::OutFormat === $key) {
×
138
                    $encodeFormat = $value;
×
UNCOV
139
                    $modified = true;
×
140

UNCOV
141
                    continue;
×
142
                }
143

144
                if (ModifierApplicatorInterface::OutQuality === $key) {
×
145
                    $encodeQuality = $value;
×
UNCOV
146
                    $modified = true;
×
147
                }
148
            }
149
        }
150

151
        return new ModifyResult(
1✔
152
            image: $image,
1✔
153
            modified: $modified,
154
            encodeFormat: $encodeFormat,
155
            encodeQuality: $encodeQuality,
156
        );
157
    }
158
}
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

© 2026 Coveralls, Inc