These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\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\Component\Addressing\Model\CountryInterface; |
||
19 | use Sylius\Component\Addressing\Model\ZoneInterface; |
||
20 | use Sylius\Component\Core\Model\AddressInterface; |
||
21 | use Sylius\Component\Variation\Generator\VariantGenerator; |
||
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('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 = []; |
||
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 | if (!method_exists($this, $method)) { |
||
90 | throw new \Exception(sprintf('Method %s does not exist', $method)); |
||
91 | } |
||
92 | |||
93 | return call_user_func_array([$this, $method], $arguments); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return VariantGenerator |
||
98 | */ |
||
99 | protected function getVariantGenerator() |
||
100 | { |
||
101 | return $this->get('sylius.generator.variant'); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get zone reference by its code. |
||
106 | * |
||
107 | * @param string $code |
||
108 | * |
||
109 | * @return ZoneInterface |
||
110 | */ |
||
111 | protected function getZoneByCode($code) |
||
112 | { |
||
113 | return $this->getReference('Sylius.Zone.'.$code); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Dispatch an event. |
||
118 | * |
||
119 | * @param string $name |
||
120 | * @param object $object |
||
121 | */ |
||
122 | protected function dispatchEvent($name, $object) |
||
123 | { |
||
124 | return $this->get('event_dispatcher')->dispatch($name, new GenericEvent($object)); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get service by id. |
||
129 | * |
||
130 | * @param string $id |
||
131 | * |
||
132 | * @return object |
||
133 | */ |
||
134 | protected function get($id) |
||
135 | { |
||
136 | return $this->container->get($id); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return AddressInterface |
||
141 | */ |
||
142 | protected function createAddress() |
||
143 | { |
||
144 | /* @var $address AddressInterface */ |
||
145 | $address = $this->getAddressFactory()->createNew(); |
||
0 ignored issues
–
show
|
|||
146 | $address->setFirstname($this->faker->firstName); |
||
147 | $address->setLastname($this->faker->lastName); |
||
148 | $address->setCity($this->faker->city); |
||
149 | $address->setStreet($this->faker->streetAddress); |
||
150 | $address->setPostcode($this->faker->postcode); |
||
151 | |||
152 | /* @var CountryInterface $country */ |
||
153 | $allCountries = Intl::getRegionBundle()->getCountryNames($this->defaultLocale); |
||
154 | $countries = array_slice($allCountries, 0, count($allCountries) - 5, true); |
||
155 | |||
156 | $countryCode = array_rand($countries); |
||
157 | $country = $this->getReference('Sylius.Country.'.$countryCode); |
||
158 | |||
159 | if ($province = $country->hasProvinces() ? $this->faker->randomElement($country->getProvinces()->toArray()) : null) { |
||
0 ignored issues
–
show
|
|||
160 | $address->setProvinceCode($province->getCode()); |
||
161 | } |
||
162 | |||
163 | $address->setCountryCode($countryCode); |
||
164 | |||
165 | return $address; |
||
166 | } |
||
167 | } |
||
168 |
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: