Completed
Push — master ( f34924...c8c8f9 )
by Paweł
13:31
created

StickerProductFixture   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 13
dl 0
loc 157
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A getName() 0 4 1
B load() 0 61 4
A configureOptionsNode() 0 7 1
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\Bundle\CoreBundle\Fixture;
13
14
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
15
use Sylius\Component\Attribute\AttributeType\TextAttributeType;
16
use Sylius\Component\Resource\Repository\RepositoryInterface;
17
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
/**
21
 * @author Kamil Kokot <[email protected]>
22
 */
23
final class StickerProductFixture extends AbstractFixture
24
{
25
    /**
26
     * @var TaxonFixture
27
     */
28
    private $taxonFixture;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $taxonRepository;
34
35
    /**
36
     * @var ProductAttributeFixture
37
     */
38
    private $productAttributeFixture;
39
40
    /**
41
     * @var ProductOptionFixture
42
     */
43
    private $productOptionFixture;
44
45
    /**
46
     * @var ProductArchetypeFixture
47
     */
48
    private $productArchetypeFixture;
49
50
    /**
51
     * @var ProductFixture
52
     */
53
    private $productFixture;
54
55
    /**
56
     * @var \Faker\Generator
57
     */
58
    private $faker;
59
60
    /**
61
     * @var OptionsResolver
62
     */
63
    private $optionsResolver;
64
65
    /**
66
     * @param TaxonFixture $taxonFixture
67
     * @param RepositoryInterface $taxonRepository
68
     * @param ProductAttributeFixture $productAttributeFixture
69
     * @param ProductOptionFixture $productOptionFixture
70
     * @param ProductArchetypeFixture $productArchetypeFixture
71
     * @param ProductFixture $productFixture
72
     */
73
    public function __construct(
74
        TaxonFixture $taxonFixture,
75
        RepositoryInterface $taxonRepository,
76
        ProductAttributeFixture $productAttributeFixture,
77
        ProductOptionFixture $productOptionFixture,
78
        ProductArchetypeFixture $productArchetypeFixture,
79
        ProductFixture $productFixture
80
    ) {
81
        $this->taxonFixture = $taxonFixture;
82
        $this->taxonRepository = $taxonRepository;
83
        $this->productAttributeFixture = $productAttributeFixture;
84
        $this->productOptionFixture = $productOptionFixture;
85
        $this->productArchetypeFixture = $productArchetypeFixture;
86
        $this->productFixture = $productFixture;
87
88
        $this->faker = \Faker\Factory::create();
89
        $this->optionsResolver =
90
            (new OptionsResolver())
91
                ->setRequired('amount')
92
                ->setAllowedTypes('amount', 'int')
93
        ;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getName()
100
    {
101
        return 'sticker_product';
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function load(array $options)
108
    {
109
        $options = $this->optionsResolver->resolve($options);
110
111
        $taxons = [];
112
        if (null === $this->taxonRepository->findOneBy(['code' => 'CATEGORY'])) {
113
            $taxons[] = ['name' => 'Category', 'code' => 'CATEGORY', 'parent' => null];
114
        }
115
116
        if (null === $this->taxonRepository->findOneBy(['code' => 'BRAND'])) {
117
            $taxons[] = ['name' => 'Brand', 'code' => 'BRAND', 'parent' => null];
118
        }
119
120
        $this->taxonFixture->load(['custom' => array_merge($taxons, [
121
            ['name' => 'Stickers', 'code' => 'STICKERS', 'parent' => 'CATEGORY'],
122
            ['name' => 'StickyPicky', 'code' => 'STICKYPICKY', 'parent' => 'BRAND'],
123
        ])]);
124
125
        $this->productAttributeFixture->load(['custom' => [
126
            ['name' => 'Sticker paper', 'code' => 'STICKER-PAPER', 'type' => TextAttributeType::TYPE],
127
            ['name' => 'Sticker resolution', 'code' => 'STICKER-RESOLUTION', 'type' => TextAttributeType::TYPE],
128
        ]]);
129
130
        $this->productOptionFixture->load(['custom' => [
131
            [
132
                'name' => 'Sticker SIZE',
133
                'code' => 'STICKER-SIZE',
134
                'values' => [
135
                    'STICKER-SIZE-3' => '3"',
136
                    'STICKER-SIZE-5' => '5"',
137
                    'STICKER-SIZE-7' => '7"',
138
                ],
139
            ],
140
        ]]);
141
142
        $this->productArchetypeFixture->load(['custom' => [
143
            [
144
                'name' => 'Sticker',
145
                'code' => 'STICKER',
146
                'product_attributes' => ['STICKER-PAPER', 'STICKER-RESOLUTION'],
147
                'product_options' => ['STICKER-SIZE'],
148
            ],
149
        ]]);
150
151
        $products = [];
152
        for ($i = 0; $i < $options['amount']; ++$i) {
153
            $products[] = [
154
                'name' => sprintf('Sticker "%s"', $this->faker->word),
155
                'code' => $this->faker->uuid,
156
                'main_taxon' => 'STICKERS',
157
                'product_archetype' => 'STICKER',
158
                'taxons' => ['STICKERS', 'STICKYPICKY'],
159
                'product_attributes' => [
160
                    'STICKER-PAPER' => sprintf('Paper from tree %s', $this->faker->randomElement(['Wung', 'Tanajno', 'Lemon-San', 'Me-Gusta'])),
0 ignored issues
show
Bug introduced by
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
161
                    'STICKER-RESOLUTION' => $this->faker->randomElement(['JKM XD', '476DPI', 'FULL HD', '200DPI']),
0 ignored issues
show
Bug introduced by
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
162
                ],
163
            ];
164
        }
165
166
        $this->productFixture->load(['custom' => $products]);
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
173
    {
174
        $optionsNode
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method min() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\FloatNodeDefinition, Symfony\Component\Config...r\IntegerNodeDefinition, Symfony\Component\Config...r\NumericNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
175
            ->children()
176
                ->integerNode('amount')->isRequired()->min(0)->end()
177
        ;
178
    }
179
}
180