Completed
Push — locale-in-url ( 6d9eda...81052c )
by Kamil
57:16 queued 38:25
created

ApiAccessTokenExampleFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ApiBundle\Fixture\Factory;
13
14
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
15
use Sylius\Bundle\ApiBundle\Model\AccessTokenInterface;
16
use Sylius\Bundle\ApiBundle\Model\ClientInterface;
17
use Sylius\Bundle\ApiBundle\Model\UserInterface;
18
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
19
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Sylius\Component\User\Repository\UserRepositoryInterface;
23
use Symfony\Component\OptionsResolver\Options;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
26
/**
27
 * @author Łukasz Chruściel <[email protected]>
28
 */
29
class ApiAccessTokenExampleFactory extends AbstractExampleFactory
30
{
31
    /**
32
     * @var FactoryInterface
33
     */
34
    private $accessTokenFactory;
35
36
    /**
37
     * @var UserRepositoryInterface
38
     */
39
    private $userRepository;
40
41
    /**
42
     * @var RepositoryInterface
43
     */
44
    private $clientRepository;
45
46
    /**
47
     * @var \Faker\Generator
48
     */
49
    private $faker;
50
51
    /**
52
     * @var OptionsResolver
53
     */
54
    private $optionsResolver;
55
56
    /**
57
     * @param FactoryInterface $accessTokenFactory
58
     * @param UserRepositoryInterface $userRepository
59
     * @param RepositoryInterface $clientRepository
60
     */
61
    public function __construct(
62
        FactoryInterface $accessTokenFactory,
63
        UserRepositoryInterface $userRepository,
64
        RepositoryInterface $clientRepository
65
    ) {
66
        $this->accessTokenFactory = $accessTokenFactory;
67
        $this->userRepository = $userRepository;
68
        $this->clientRepository = $clientRepository;
69
70
        $this->faker = \Faker\Factory::create();
71
        $this->optionsResolver = new OptionsResolver();
72
73
        $this->configureOptions($this->optionsResolver);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function create(array $options = [])
80
    {
81
        $options = $this->optionsResolver->resolve($options);
82
83
        /** @var AccessTokenInterface $accessToken */
84
        $accessToken = $this->accessTokenFactory->createNew();
85
86
        $accessToken->setClient($options['client']);
87
        $accessToken->setToken($options['token']);
88
        $accessToken->setUser($options['user']);
89
90
        if (isset($options['expires_at'])) {
91
            $accessToken->setExpiresAt($options['expires_at']);
92
        }
93
94
        return $accessToken;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function configureOptions(OptionsResolver $resolver)
101
    {
102
        $resolver
103
            ->setDefault('user', LazyOption::randomOne($this->userRepository))
104
            ->setAllowedTypes('user', ['string', UserInterface::class, 'null'])
105
            ->setNormalizer('user', function (Options $options, $userEmail) {
106
                return $this->userRepository->findOneByEmail($userEmail);
107
            })
108
            ->setDefault('token', function (Options $options) {
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...
109
                return $this->faker->md5;
110
            })
111
            ->setDefault('client', LazyOption::randomOne($this->clientRepository))
112
            ->setAllowedTypes('client', ['string', ClientInterface::class, 'null'])
113
            ->setNormalizer('client', LazyOption::findOneBy($this->clientRepository, 'randomId'))
114
            ->setDefault('expires_at', null)
115
            ->setAllowedTypes('expires_at', ['null', \DateTime::class])
116
        ;
117
    }
118
}
119