Completed
Push — master ( 2f9340...094334 )
by Kamil
17:24
created

DefaultFranceChannelFactory::createCountry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\Test\Services;
13
14
use Sylius\Component\Addressing\Model\ZoneInterface;
15
use Sylius\Component\Addressing\Model\ZoneMemberInterface;
16
use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
17
use Sylius\Component\Core\Model\ChannelInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 */
24
class DefaultFranceChannelFactory implements DefaultStoreDataInterface
25
{
26
    const DEFAULT_CHANNEL_CODE = 'WEB-FR';
27
    const DEFAULT_ZONE_CODE = 'FR';
28
    const DEFAULT_ZONE_NAME = 'France';
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $channelRepository;
34
35
    /**
36
     * @var RepositoryInterface
37
     */
38
    private $zoneMemberRepository;
39
40
    /**
41
     * @var RepositoryInterface
42
     */
43
    private $zoneRepository;
44
45
    /**
46
     * @var ChannelFactoryInterface
47
     */
48
    private $channelFactory;
49
50
    /**
51
     * @var FactoryInterface
52
     */
53
    private $zoneMemberFactory;
54
55
    /**
56
     * @var FactoryInterface
57
     */
58
    private $zoneFactory;
59
60
    /**
61
     * @param RepositoryInterface $channelRepository
62
     * @param RepositoryInterface $zoneMemberRepository
63
     * @param RepositoryInterface $zoneRepository
64
     * @param FactoryInterface $channelFactory
65
     * @param FactoryInterface $zoneMemberFactory
66
     * @param FactoryInterface $zoneFactory
67
     */
68
    public function __construct(
69
        RepositoryInterface $channelRepository,
70
        RepositoryInterface $zoneMemberRepository,
71
        RepositoryInterface $zoneRepository,
72
        FactoryInterface $channelFactory,
73
        FactoryInterface $zoneMemberFactory,
74
        FactoryInterface $zoneFactory
75
    ) {
76
        $this->channelRepository = $channelRepository;
77
        $this->zoneMemberRepository = $zoneMemberRepository;
78
        $this->zoneRepository = $zoneRepository;
79
        $this->channelFactory = $channelFactory;
0 ignored issues
show
Documentation Bug introduced by
$channelFactory is of type object<Sylius\Component\...ctory\FactoryInterface>, but the property $channelFactory was declared to be of type object<Sylius\Component\...hannelFactoryInterface>. 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...
80
        $this->zoneMemberFactory = $zoneMemberFactory;
81
        $this->zoneFactory = $zoneFactory;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function create()
88
    {
89
        $defaultData['channel'] = $this->createChannel();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$defaultData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaultData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
90
        $defaultData['zone_member'] = $this->createZoneMember();
91
        $defaultData['zone'] = $this->createZone($defaultData['zone_member']);
92
93
        $this->channelRepository->add($defaultData['channel']);
94
        $this->zoneRepository->add($defaultData['zone']);
95
        $this->zoneMemberRepository->add($defaultData['zone_member']);
96
97
        return $defaultData;
98
    }
99
100
    /**
101
     * @return ChannelInterface
102
     */
103
    private function createChannel()
104
    {
105
        $channel = $this->channelFactory->createNamed('France');
106
        $channel->setCode(self::DEFAULT_CHANNEL_CODE);
107
108
        return $channel;
109
    }
110
111
    /**
112
     * @return ZoneMemberInterface
113
     */
114
    private function createZoneMember()
115
    {
116
        $zoneMember = $this->zoneMemberFactory->createNew();
117
        $zoneMember->setCode(self::DEFAULT_ZONE_CODE);
118
119
        return $zoneMember;
120
    }
121
122
    /**
123
     * @param ZoneMemberInterface $zoneMember
124
     *
125
     * @return ZoneInterface
126
     */
127
    private function createZone(ZoneMemberInterface $zoneMember)
128
    {
129
        $zone = $this->zoneFactory->createNew();
130
        $zone->setCode(self::DEFAULT_ZONE_CODE);
131
        $zone->setName(self::DEFAULT_ZONE_NAME);
132
        $zone->setType(ZoneInterface::TYPE_COUNTRY);
133
        $zone->addMember($zoneMember);
134
135
        return $zone;
136
    }
137
}
138