JsonFilesystemImporter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A execute() 0 8 2
1
<?php
2
3
/**
4
 * This file is part of the Setup package.
5
 *
6
 * (c) Sitewards GmbH
7
 */
8
9
namespace Sitewards\Setup\Service\Page;
10
11
use JMS\Serializer\Serializer;
12
use Sitewards\Setup\Domain\Page\PageRepositoryInterface;
13
14
final class JsonFilesystemImporter implements ImporterInterface
15
{
16
    /** @var string */
17
    private $filename = 'pages.json';
18
19
    /** @var PageRepositoryInterface */
20
    private $pageRepository;
21
22
    /** @var Serializer */
23
    private $serializer;
24
25
    /**
26
     * @param PageRepositoryInterface $pageRepository
27
     * @param Serializer $serializer
28
     */
29
    public function __construct(
30
        PageRepositoryInterface $pageRepository,
31
        Serializer $serializer
32
    )
33
    {
34
        $this->pageRepository = $pageRepository;
35
        $this->serializer     = $serializer;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function execute()
42
    {
43
        $pages = $this->serializer->deserialize(file_get_contents($this->filename), 'array<Sitewards\Setup\Domain\Page\Page>', 'json');
44
45
        foreach ($pages as $page) {
0 ignored issues
show
Bug introduced by
The expression $pages of type object|array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46
            $this->pageRepository->save($page);
47
        }
48
    }
49
}
50