Completed
Push — master ( 1714a8...2dab78 )
by Michał
11:58
created

DataFixture::getZoneMemberFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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\FixturesBundle\DataFixtures;
13
14
use Doctrine\Common\DataFixtures\AbstractFixture;
15
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
16
use Faker\Factory as FakerFactory;
17
use Faker\Generator;
18
use Sylius\Bundle\ProductBundle\Generator\VariantGenerator;
19
use Sylius\Component\Addressing\Model\CountryInterface;
20
use Sylius\Component\Addressing\Model\ZoneInterface;
21
use Sylius\Component\Core\Model\AddressInterface;
22
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use Symfony\Component\EventDispatcher\GenericEvent;
25
use Symfony\Component\Intl\Intl;
26
27
/**
28
 * Base data fixture.
29
 *
30
 * @author Paweł Jędrzejewski <[email protected]>
31
 * @author Gonzalo Vilaseca <[email protected]>
32
 */
33
abstract class DataFixture extends AbstractFixture implements ContainerAwareInterface, OrderedFixtureInterface
34
{
35
    /**
36
     * Container.
37
     *
38
     * @var ContainerInterface
39
     */
40
    protected $container;
41
42
    /**
43
     * Alias to default language faker.
44
     *
45
     * @var Generator
46
     */
47
    protected $faker;
48
49
    /**
50
     * Faker.
51
     *
52
     * @var Generator
53
     */
54
    protected $fakers;
55
56
    /**
57
     * Default locale.
58
     *
59
     * @var string
60
     */
61
    protected $defaultLocale;
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function setContainer(ContainerInterface $container = null)
67
    {
68
        $this->container = $container;
69
70
        if (null !== $container) {
71
            $this->defaultLocale = $container->getParameter('sylius.locale');
72
            $this->fakers[$this->defaultLocale] = FakerFactory::create($this->defaultLocale);
73
            $this->faker = $this->fakers[$this->defaultLocale];
74
        }
75
76
        $this->fakers['es_ES'] = FakerFactory::create('es_ES');
77
    }
78
79
    public function __call($method, $arguments)
80
    {
81
        $matches = array();
82
        if (preg_match('/^get(.*)Repository$/', $method, $matches)) {
83
            return $this->get('sylius.repository.'.$matches[1]);
84
        }
85
        if (preg_match('/^get(.*)Factory$/', $method, $matches)) {
86
            return $this->get('sylius.factory.'.$matches[1]);
87
        }
88
89
        return call_user_func_array(array($this, $method), $arguments);
90
    }
91
92
    protected function getZoneMemberFactory($zoneType)
93
    {
94
        return $this->get('sylius.factory.zone_member_'.$zoneType);
95
    }
96
97
    /**
98
     * @return VariantGenerator
99
     */
100
    protected function getVariantGenerator()
101
    {
102
        return $this->get('sylius.generator.product_variant');
103
    }
104
105
    /**
106
     * Get zone reference by its name.
107
     *
108
     * @param string $name
109
     *
110
     * @return ZoneInterface
111
     */
112
    protected function getZoneByName($name)
113
    {
114
        return $this->getReference('Sylius.Zone.'.$name);
115
    }
116
117
    /**
118
     * Dispatch an event.
119
     *
120
     * @param string $name
121
     * @param object $object
122
     */
123
    protected function dispatchEvent($name, $object)
124
    {
125
        return $this->get('event_dispatcher')->dispatch($name, new GenericEvent($object));
126
    }
127
128
    /**
129
     * Get service by id.
130
     *
131
     * @param string $id
132
     *
133
     * @return object
134
     */
135
    protected function get($id)
136
    {
137
        return $this->container->get($id);
138
    }
139
140
    /**
141
     * @return AddressInterface
142
     */
143
    protected function createAddress()
144
    {
145
        /* @var $address AddressInterface */
146
        $address = $this->getAddressFactory()->createNew();
0 ignored issues
show
Documentation Bug introduced by
The method getAddressFactory does not exist on object<Sylius\Bundle\Fix...taFixtures\DataFixture>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
147
        $address->setFirstname($this->faker->firstName);
148
        $address->setLastname($this->faker->lastName);
149
        $address->setCity($this->faker->city);
150
        $address->setStreet($this->faker->streetAddress);
151
        $address->setPostcode($this->faker->postcode);
152
153
        /** @var CountryInterface $country */
154
        $countries = Intl::getRegionBundle()->getCountryNames($this->defaultLocale);
155
        $isoName = array_rand($countries);
156
        $country = $this->getReference("Sylius.Country." . $isoName);
157
158
        $province = $country->hasProvinces() ? $this->faker->randomElement($country->getProvinces()->toArray()) : null;
0 ignored issues
show
Bug introduced by
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
159
160
        $address->setCountry($country);
161
        $address->setProvince($province);
162
163
        return $address;
164
    }
165
}
166