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

heimrichhannot / contao-utils-bundle / 11857290883

15 Nov 2024 01:38PM UTC coverage: 24.087% (+0.8%) from 23.297%
11857290883

push

github

web-flow
Backend UI Util (#86)

* add backend wizard ui with popup wizard method

* added tests, update workflwo

* fix tests

* enhance test coverage

* adjust styling, renamed variable

* raise code coverage, fix bug

59 of 59 new or added lines in 3 files covered. (100.0%)

1379 of 5725 relevant lines covered (24.09%)

1.56 hits per line

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

0.0
/src/Command/CreateImageSizeItemsCommand.php
1
<?php
2

3
/*
4
 * Copyright (c) 2022 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Command;
10

11
use Contao\CoreBundle\Framework\ContaoFramework;
12
use Contao\ImageSizeItemModel;
13
use Contao\ImageSizeModel;
14
use Contao\Model;
15
use HeimrichHannot\UtilsBundle\Util\Utils;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Style\SymfonyStyle;
21

22
class CreateImageSizeItemsCommand extends Command
23
{
24
    const MODE_FIRST = 1;
25
    const MODE_INTERMEDIATE = 2;
26
    const MODE_LAST = 3;
27

28
    const DEFAULT_BREAKPOINTS = [
29
        576,
30
        768,
31
        992,
32
        1200,
33
        1400,
34
    ];
35

36
    protected static $defaultName = 'huh:utils:create-image-size-items';
37
    /**
38
     * @var ContaoFramework
39
     */
40
    private $contaoFramework;
41
    /**
42
     * @var Utils
43
     */
44
    private $utils;
45

46
    public function __construct(ContaoFramework $contaoFramework, Utils $utils)
47
    {
48
        parent::__construct();
×
49
        $this->contaoFramework = $contaoFramework;
×
50
        $this->utils = $utils;
×
51
    }
52

53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function configure()
57
    {
58
        $this
×
59
            ->setDescription('Creates image size items for a given image size entity. Image size entities with existing image size items will be skipped.')
×
60
            ->addArgument(
×
61
                'image-size-ids',
×
62
                InputArgument::OPTIONAL,
×
63
                'The comma separated ids of the image size. Skip the parameter in order to create image size items for all image size entities.'
×
64
            )
×
65
            ->addArgument(
×
66
                'breakpoints',
×
67
                InputArgument::OPTIONAL,
×
68
                'The comma separated breakpoints as pixel amounts (defaults to "576,768,992,1200,1400").', implode(',', static::DEFAULT_BREAKPOINTS)
×
69
            );
×
70
    }
71

72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function execute(InputInterface $input, OutputInterface $output)
76
    {
77
        $this->contaoFramework->initialize();
×
78

79
        $io = new SymfonyStyle($input, $output);
×
80

81
        // prevent sql injection
82
        $imageSizeIds = preg_replace('@[^0-9,]@i', '', $input->getArgument('image-size-ids'));
×
83

84
        $imageSizeIds = explode(',', $imageSizeIds);
×
85
        $breakpoints = explode(',', $input->getArgument('breakpoints'));
×
86

87
        $this->createImageSizes($io, $breakpoints, $imageSizeIds);
×
88

89
        return 0;
×
90
    }
91

92
    /**
93
     * @param ImageSizeModel $imageSize
94
     */
95
    protected function createItem(Model $imageSize, int $index, int $breakpoint, ?int $nextBreakpoint, int $mode)
96
    {
97
        $item = new ImageSizeItemModel();
×
98

99
        $item->tstamp = time();
×
100
        $item->pid = $imageSize->id;
×
101
        $item->sorting = 128 * $index;
×
102
        $item->densities = $imageSize->densities;
×
103
        $item->resizeMode = $imageSize->resizeMode;
×
104

105
        switch ($mode) {
106
            case static::MODE_FIRST:
×
107
                $item->media = '(max-width: '.($breakpoint - 1).'px)';
×
108
                $item->width = $breakpoint - 1;
×
109

110
                break;
×
111

112
            case static::MODE_INTERMEDIATE:
×
113
                $item->media = '(min-width: '.$breakpoint.'px) and (max-width: '.($nextBreakpoint - 1).'px)';
×
114
                $item->width = $nextBreakpoint - 1;
×
115

116
                break;
×
117

118
            case static::MODE_LAST:
×
119
                $item->media = '(min-width: '.$breakpoint.'px)';
×
120
                $item->width = max($breakpoint, $imageSize->width);
×
121

122
                break;
×
123
        }
124

125
        $item->height = ($item->width * $imageSize->height) / $imageSize->width;
×
126

127
        $item->save();
×
128
    }
129

130
    private function createImageSizes(SymfonyStyle $io, array $breakpoints, array $imageSizeIds = [])
131
    {
132
        sort($breakpoints);
×
133

134
        $creationCount = 0;
×
135
        $columns = (empty($imageSizeIds) ? [] : ['tl_image_size.id IN ('.implode(',', $imageSizeIds).')']);
×
136

137
        if (null === ($imageSizes = $this->utils->model()->findModelInstancesBy('tl_image_size', $columns, []))) {
×
138
            $io->error('No image sizes found for the given ids.');
×
139

140
            return false;
×
141
        }
142

143
        /** @var ImageSizeModel $imageSize */
144
        foreach ($imageSizes as $imageSize) {
×
145
            $existingItems = $this->utils->model()->findModelInstancesBy('tl_image_size_item', ['tl_image_size_item.pid=?'], [$imageSize->id]);
×
146

147
            if (null !== $existingItems) {
×
148
                $io->warning('Skipping image size ID '.$imageSize->id.' because it already has existing image size items.');
×
149

150
                continue;
×
151
            }
152

153
            $j = 0;
×
154

155
            // first
156
            $this->createItem($imageSize, $j++, $breakpoints[0], null, static::MODE_FIRST);
×
157
            ++$creationCount;
×
158

159
            // intermediates
160
            foreach ($breakpoints as $i => $breakpoint) {
×
161
                if ($i === \count($breakpoints) - 1) {
×
162
                    continue;
×
163
                }
164

165
                $this->createItem($imageSize, $j++, $breakpoint, $breakpoints[$i + 1], static::MODE_INTERMEDIATE);
×
166

167
                ++$creationCount;
×
168
            }
169

170
            // last
171
            $this->createItem($imageSize, $j++, $breakpoints[\count($breakpoints) - 1], null, static::MODE_LAST);
×
172
            ++$creationCount;
×
173
        }
174

175
        $io->success($creationCount.' image size items have been created.');
×
176

177
        return true;
×
178
    }
179
}
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