Completed
Push — 6.7 ( caaa8b...485307 )
by
unknown
19:19
created

ImageFileLister   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A countFiles() 0 4 1
B loadMetadataList() 0 28 5
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Bundle\EzPublishIOBundle\Migration\FileLister;
8
9
use eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator;
10
use eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPurger\ImageFileList;
11
use eZ\Bundle\EzPublishIOBundle\ApiLoader\HandlerFactory;
12
use eZ\Bundle\EzPublishIOBundle\Migration\FileListerInterface;
13
use eZ\Bundle\EzPublishIOBundle\Migration\MigrationHandler;
14
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException;
15
use Iterator;
16
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
17
use LimitIterator;
18
use Psr\Log\LoggerInterface;
19
20
class ImageFileLister extends MigrationHandler implements FileListerInterface
21
{
22
    /** @var ImageFileList */
23
    private $imageFileList;
24
25
    /** @var \eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator */
26
    private $variationPathGenerator;
27
28
    /** @var \Liip\ImagineBundle\Imagine\Filter\FilterConfiguration */
29
    private $filterConfiguration;
30
31
    /** @var string Directory where images are stored, within the storage dir. Example: 'images' */
32
    private $imagesDir;
33
34
    /**
35
     * @param \eZ\Bundle\EzPublishIOBundle\ApiLoader\HandlerFactory $metadataHandlerFactory
36
     * @param \eZ\Bundle\EzPublishIOBundle\ApiLoader\HandlerFactory $binarydataHandlerFactory
37
     * @param \Psr\Log\LoggerInterface $logger
38
     * @param \Iterator $imageFileList
39
     * @param \eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator
40
     * @param \Liip\ImagineBundle\Imagine\Filter\FilterConfiguration
41
     * @param string $imagesDir Directory where images are stored, within the storage dir. Example: 'images'
42
     */
43
    public function __construct(
44
        HandlerFactory $metadataHandlerFactory,
45
        HandlerFactory $binarydataHandlerFactory,
46
        LoggerInterface $logger = null,
47
        Iterator $imageFileList,
48
        VariationPathGenerator $variationPathGenerator,
49
        FilterConfiguration $filterConfiguration,
50
        $imagesDir
51
    ) {
52
        $this->imageFileList = $imageFileList;
0 ignored issues
show
Documentation Bug introduced by
$imageFileList is of type object<Iterator>, but the property $imageFileList was declared to be of type object<eZ\Bundle\EzPubli...onPurger\ImageFileList>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
53
        $this->variationPathGenerator = $variationPathGenerator;
54
        $this->filterConfiguration = $filterConfiguration;
55
        $this->imagesDir = $imagesDir;
56
57
        $this->imageFileList->rewind();
58
59
        parent::__construct($metadataHandlerFactory, $binarydataHandlerFactory, $logger);
60
    }
61
62
    public function countFiles()
63
    {
64
        return count($this->imageFileList);
65
    }
66
67
    public function loadMetadataList($limit = null, $offset = null)
68
    {
69
        $metadataList = [];
70
        $imageLimitList = new LimitIterator($this->imageFileList, $offset, $limit);
71
        $aliasNames = array_keys($this->filterConfiguration->all());
72
73
        foreach ($imageLimitList as $originalImageId) {
74
            try {
75
                $metadataList[] = $this->fromMetadataHandler->load($this->imagesDir . '/' . $originalImageId);
76
            } catch (BinaryFileNotFoundException $e) {
77
                $this->logMissingFile($originalImageId);
78
79
                continue;
80
            }
81
82
            foreach ($aliasNames as $aliasName) {
83
                $variationImageId = $this->variationPathGenerator->getVariationPath($originalImageId, $aliasName);
84
85
                try {
86
                    $metadataList[] = $this->fromMetadataHandler->load($this->imagesDir . '/' . $variationImageId);
87
                } catch (BinaryFileNotFoundException $e) {
88
                    $this->logMissingFile($variationImageId);
89
                }
90
            }
91
        }
92
93
        return $metadataList;
94
    }
95
}
96