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.

BannerExampleFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 4
dl 0
loc 15
rs 10
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
            $banner->setMainText($this->faker->sentence(4));
102
            $banner->setSecondaryText($this->faker->sentence(9));
103
104
            $banner->setImageFile($this->createImage($options['image']));
105
            $banner->setMobileImageFile($this->createImage($options['mobile_image']));
106
        }
107
108
        return $banner;
109
    }
110
111
    /**
112
     * @param string $imagePath
113
     * @return UploadedFile
114
     */
115
    private function createImage(string $imagePath): UploadedFile
116
    {
117
        $imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath);
118
        if (is_array($imagePath) && count($imagePath) > 0) {
119
            $imagePath = $imagePath[0];
120
        }
121
122
        return new UploadedFile($imagePath, basename($imagePath));
123
    }
124
125
    /**
126
     * @return Generator
127
     */
128
    private function getLocales(): Generator
129
    {
130
        /** @var LocaleInterface[] $locales */
131
        $locales = $this->localeRepository->findAll();
132
        foreach ($locales as $locale) {
133
            yield $locale->getCode();
134
        }
135
    }
136
}
137