Passed
Push — master ( 94f019...83575f )
by
unknown
04:57
created

VendorExampleFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 106
rs 10
c 1
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 25 3
A createImage() 0 7 4
A __construct() 0 15 1
A configureOptions() 0 11 1
A getLocales() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusVendorPlugin\Fixture\Factory;
6
7
use Faker\Factory;
8
use Generator;
9
use Odiseo\SyliusVendorPlugin\Entity\VendorInterface;
10
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
11
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
12
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
13
use Sylius\Component\Locale\Model\LocaleInterface;
14
use Sylius\Component\Resource\Factory\FactoryInterface;
15
use Sylius\Component\Resource\Repository\RepositoryInterface;
16
use Symfony\Component\Config\FileLocatorInterface;
17
use Symfony\Component\HttpFoundation\File\UploadedFile;
18
use Symfony\Component\OptionsResolver\Options;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
final class VendorExampleFactory extends AbstractExampleFactory
22
{
23
    /** @var FactoryInterface */
24
    private $vendorFactory;
25
26
    /** @var ChannelRepositoryInterface */
27
    private $channelRepository;
28
29
    /** @var RepositoryInterface */
30
    private $localeRepository;
31
32
    /** @var \Faker\Generator */
33
    private $faker;
34
35
    /** @var FileLocatorInterface|null */
36
    private $fileLocator;
37
38
    /** @var OptionsResolver */
39
    private $optionsResolver;
40
41
    public function __construct(
42
        FactoryInterface $vendorFactory,
43
        ChannelRepositoryInterface $channelRepository,
44
        RepositoryInterface $localeRepository,
45
        ?FileLocatorInterface $fileLocator = null
46
    ) {
47
        $this->vendorFactory = $vendorFactory;
48
        $this->channelRepository = $channelRepository;
49
        $this->localeRepository = $localeRepository;
50
        $this->fileLocator = $fileLocator;
51
52
        $this->faker = Factory::create();
53
        $this->optionsResolver = new OptionsResolver();
54
55
        $this->configureOptions($this->optionsResolver);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function configureOptions(OptionsResolver $resolver): void
62
    {
63
        $resolver
64
            ->setDefault('channels', LazyOption::randomOnes($this->channelRepository, 3))
65
            ->setAllowedTypes('channels', 'array')
66
            ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))
67
68
            ->setDefault('logo', function (Options $options): string {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
            ->setDefault('logo', function (/** @scrutinizer ignore-unused */ Options $options): string {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
                return __DIR__.'/../../Resources/fixtures/vendor/0'.rand(1, 4).'.png';
70
            })
71
            ->setAllowedTypes('logo', ['string'])
72
        ;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function create(array $options = []): VendorInterface
79
    {
80
        $options = $this->optionsResolver->resolve($options);
81
82
        /** @var VendorInterface $vendor */
83
        $vendor = $this->vendorFactory->createNew();
84
        $vendor->setName($this->faker->company);
85
        $vendor->setSlug($this->faker->slug);
86
        $vendor->setEmail($this->faker->companyEmail);
87
88
        foreach ($options['channels'] as $channel) {
89
            $vendor->addChannel($channel);
90
        }
91
92
        /** @var string $localeCode */
93
        foreach ($this->getLocales() as $localeCode) {
94
            $vendor->setCurrentLocale($localeCode);
95
            $vendor->setFallbackLocale($localeCode);
96
97
            $vendor->setDescription($this->faker->text);
98
        }
99
100
        $vendor->setLogoFile($this->createImage($options['logo']));
101
102
        return $vendor;
103
    }
104
105
    /**
106
     * @param string $imagePath
107
     * @return UploadedFile
108
     */
109
    private function createImage(string $imagePath): UploadedFile
110
    {
111
        $imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath);
112
        if(is_array($imagePath) && count($imagePath) > 0)
113
            $imagePath = $imagePath[0];
114
115
        return new UploadedFile($imagePath, basename($imagePath));
116
    }
117
118
    /**
119
     * @return Generator
120
     */
121
    private function getLocales(): Generator
122
    {
123
        /** @var LocaleInterface[] $locales */
124
        $locales = $this->localeRepository->findAll();
125
        foreach ($locales as $locale) {
126
            yield $locale->getCode();
127
        }
128
    }
129
}
130