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.
Passed
Push — master ( af736d...64dffd )
by Odiseo
09:41
created

BannerExampleFactory::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 16
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusBannerPlugin\Fixture\Factory;
6
7
use Faker\Factory;
8
use Generator;
9
use Odiseo\SyliusBannerPlugin\Entity\BannerInterface;
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 BannerExampleFactory extends AbstractExampleFactory
22
{
23
    /** @var FactoryInterface */
24
    private $bannerFactory;
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 $bannerFactory,
43
        ChannelRepositoryInterface $channelRepository,
44
        RepositoryInterface $localeRepository,
45
        ?FileLocatorInterface $fileLocator = null
46
    ) {
47
        $this->bannerFactory = $bannerFactory;
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('image', 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('image', 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/banner/images/0'.rand(1, 4).'.png';
70
            })
71
            ->setAllowedTypes('image', ['string'])
72
73
            ->setDefault('mobile_image', 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

73
            ->setDefault('mobile_image', 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...
74
                return __DIR__.'/../../Resources/fixtures/banner/mobile-images/0'.rand(1, 4).'.png';
75
            })
76
            ->setAllowedTypes('mobile_image', ['string'])
77
        ;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function create(array $options = []): BannerInterface
84
    {
85
        $options = $this->optionsResolver->resolve($options);
86
87
        /** @var BannerInterface $banner */
88
        $banner = $this->bannerFactory->createNew();
89
        $banner->setCode($this->faker->slug);
90
91
        foreach ($options['channels'] as $channel) {
92
            $banner->addChannel($channel);
93
        }
94
95
        /** @var string $localeCode */
96
        foreach ($this->getLocales() as $localeCode) {
97
            $banner->setCurrentLocale($localeCode);
98
            $banner->setFallbackLocale($localeCode);
99
100
            $banner->setUrl($this->faker->url);
101
102
            $banner->setImageFile($this->createImage($options['image']));
103
            $banner->setMobileImageFile($this->createImage($options['mobile_image']));
104
        }
105
106
        return $banner;
107
    }
108
109
    /**
110
     * @param string $imagePath
111
     * @return UploadedFile
112
     */
113
    private function createImage(string $imagePath): UploadedFile
114
    {
115
        $imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath);
116
117
        return new UploadedFile($imagePath, basename($imagePath));
0 ignored issues
show
Bug introduced by
It seems like $imagePath can also be of type array; however, parameter $path of basename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

117
        return new UploadedFile($imagePath, basename(/** @scrutinizer ignore-type */ $imagePath));
Loading history...
118
    }
119
120
    /**
121
     * @return Generator
122
     */
123
    private function getLocales(): Generator
124
    {
125
        /** @var LocaleInterface[] $locales */
126
        $locales = $this->localeRepository->findAll();
127
        foreach ($locales as $locale) {
128
            yield $locale->getCode();
129
        }
130
    }
131
}
132