Completed
Push — master ( 4a7fd0...bb86ee )
by Loïc
13s queued 10s
created

ApiClientExampleFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 6
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 14 1
A configureOptions() 0 13 1
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
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 App\Fixture\Factory;
13
14
use FOS\OAuthServerBundle\Model\ClientInterface;
15
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
16
use Symfony\Component\OptionsResolver\Options;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
class ApiClientExampleFactory extends AbstractExampleFactory
20
{
21
    /** @var ClientManagerInterface */
22
    private $clientManager;
23
24
    /** @var \Faker\Generator */
25
    private $faker;
26
27
    /** @var OptionsResolver */
28
    private $optionsResolver;
29
30
    public function __construct(ClientManagerInterface $clientManager)
31
    {
32
        $this->clientManager = $clientManager;
33
34
        $this->faker = \Faker\Factory::create();
35
        $this->optionsResolver = new OptionsResolver();
36
37
        $this->configureOptions($this->optionsResolver);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function create(array $options = []): ClientInterface
44
    {
45
        $options = $this->optionsResolver->resolve($options);
46
47
        /** @var ClientInterface $client */
48
        $client = $this->clientManager->createClient();
49
50
        $client->setRandomId($options['random_id']);
51
        $client->setSecret($options['secret']);
52
53
        $client->setAllowedGrantTypes($options['allowed_grant_types']);
54
55
        return $client;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function configureOptions(OptionsResolver $resolver): void
62
    {
63
        $resolver
64
            ->setDefault('random_id', function (Options $options): int {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

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

Loading history...
65
                return $this->faker->unique()->randomNumber(8);
66
            })
67
            ->setDefault('secret', function (Options $options): string {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

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

Loading history...
68
                return $this->faker->uuid;
69
            })
70
            ->setDefault('allowed_grant_types', [])
71
            ->setAllowedTypes('allowed_grant_types', ['array'])
72
        ;
73
    }
74
}
75