GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

VendorExampleFactory::getLocales()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
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 Odiseo\SyliusVendorPlugin\Uploader\VendorLogoUploaderInterface;
11
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
12
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
13
use Sylius\Component\Core\Formatter\StringInflector;
14
use Sylius\Component\Locale\Model\LocaleInterface;
15
use Sylius\Component\Resource\Factory\FactoryInterface;
16
use Sylius\Component\Resource\Repository\RepositoryInterface;
17
use Symfony\Component\Config\FileLocatorInterface;
18
use Symfony\Component\HttpFoundation\File\UploadedFile;
19
use Symfony\Component\OptionsResolver\Options;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
22
class VendorExampleFactory implements ExampleFactoryInterface
23
{
24
    /** @var FactoryInterface */
25
    protected $vendorFactory;
26
27
    /** @var VendorLogoUploaderInterface */
28
    protected $vendorLogoUploader;
29
30
    /** @var RepositoryInterface */
31
    protected $channelRepository;
32
33
    /** @var RepositoryInterface */
34
    protected $productRepository;
35
36
    /** @var RepositoryInterface */
37
    protected $localeRepository;
38
39
    /** @var \Faker\Generator */
40
    protected $faker;
41
42
    /** @var FileLocatorInterface|null */
43
    protected $fileLocator;
44
45
    /** @var OptionsResolver */
46
    protected $optionsResolver;
47
48
    public function __construct(
49
        FactoryInterface $vendorFactory,
50
        VendorLogoUploaderInterface $vendorLogoUploader,
51
        RepositoryInterface $channelRepository,
52
        RepositoryInterface $productRepository,
53
        RepositoryInterface $localeRepository,
54
        ?FileLocatorInterface $fileLocator = null
55
    ) {
56
        $this->vendorFactory = $vendorFactory;
57
        $this->vendorLogoUploader = $vendorLogoUploader;
58
        $this->channelRepository = $channelRepository;
59
        $this->productRepository = $productRepository;
60
        $this->localeRepository = $localeRepository;
61
        $this->fileLocator = $fileLocator;
62
63
        $this->faker = Factory::create();
64
        $this->optionsResolver = new OptionsResolver();
65
66
        $this->configureOptions($this->optionsResolver);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function create(array $options = []): VendorInterface
73
    {
74
        $options = $this->optionsResolver->resolve($options);
75
76
        /** @var VendorInterface $vendor */
77
        $vendor = $this->vendorFactory->createNew();
78
        $vendor->setName($options['name']);
79
        $vendor->setEmail($options['email']);
80
81
        /** @var string $localeCode */
82
        foreach ($this->getLocales() as $localeCode) {
83
            $vendor->setCurrentLocale($localeCode);
84
            $vendor->setFallbackLocale($localeCode);
85
86
            $vendor->setDescription($options['description']);
87
            $vendor->setSlug($options['slug']);
88
        }
89
90
        foreach ($options['channels'] as $channel) {
91
            $vendor->addChannel($channel);
92
        }
93
94
        foreach ($options['products'] as $product) {
95
            $vendor->addProduct($product);
96
        }
97
98
        $vendor->setLogoFile($this->createImage($options['logo']));
99
100
        $this->vendorLogoUploader->upload($vendor);
101
102
        return $vendor;
103
    }
104
105
    protected function createImage(string $imagePath): UploadedFile
106
    {
107
        /** @var string $imagePath */
108
        $imagePath = null === $this->fileLocator ? $imagePath : $this->fileLocator->locate($imagePath);
109
110
        return new UploadedFile($imagePath, basename($imagePath));
111
    }
112
113
    /**
114
     * @return Generator
115
     */
116
    protected function getLocales(): Generator
117
    {
118
        /** @var LocaleInterface[] $locales */
119
        $locales = $this->localeRepository->findAll();
120
        foreach ($locales as $locale) {
121
            yield $locale->getCode();
122
        }
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    protected function configureOptions(OptionsResolver $resolver): void
129
    {
130
        $resolver
131
            ->setDefault('channels', LazyOption::randomOnes($this->channelRepository, 3))
132
            ->setAllowedTypes('channels', 'array')
133
            ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))
134
            ->setDefault('products', LazyOption::randomOnes($this->productRepository, 3))
135
            ->setAllowedTypes('products', 'array')
136
            ->setNormalizer('products', LazyOption::findBy($this->productRepository, 'code'))
137
            ->setRequired('name')
138
            ->setAllowedTypes('name', 'string')
139
            ->setDefault('name', 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

139
            ->setDefault('name', 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...
140
                return $this->faker->company;
141
            })
142
            ->setRequired('slug')
143
            ->setAllowedTypes('slug', 'string')
144
            ->setDefault('slug', function (Options $options): string {
145
                return StringInflector::nameToCode((string) $options['name']);
146
            })
147
            ->setRequired('email')
148
            ->setAllowedTypes('email', 'string')
149
            ->setDefault('email', 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

149
            ->setDefault('email', 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...
150
                return $this->faker->companyEmail;
151
            })
152
            ->setRequired('logo')
153
            ->setAllowedTypes('logo', 'string')
154
            ->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

154
            ->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...
155
                return __DIR__.'/../../Resources/fixtures/vendor/images/0'.rand(1, 3).'.png';
156
            })
157
            ->setRequired('description')
158
            ->setAllowedTypes('description', 'string')
159
            ->setDefault('description', 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

159
            ->setDefault('description', 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...
160
                return $this->faker->text;
161
            })
162
        ;
163
    }
164
}
165