Completed
Push — master ( 648607...0311ff )
by Paweł
28:06 queued 12:04
created

Bundle/CoreBundle/DataFixtures/ORM/LoadApiData.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\CoreBundle\DataFixtures\ORM;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
16
use OAuth2\OAuth2;
17
use Sylius\Bundle\ApiBundle\Model\Client;
18
use Sylius\Bundle\CoreBundle\DataFixtures\DataFixture;
19
use Sylius\Component\Core\Model\UserInterface;
20
21
/**
22
 * Api fixtures.
23
 *
24
 * @author Michał Marcinkowski <[email protected]>
25
 */
26
class LoadApiData extends DataFixture
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function load(ObjectManager $manager)
32
    {
33
        // create user with API role
34
        $user = $this->createUser(
35
            '[email protected]',
36
            'api',
37
            true,
38
            ['ROLE_API_ACCESS']
39
        );
40
        $user->addAuthorizationRole($this->get('sylius.repository.role')->findOneBy(['code' => 'administrator']));
41
42
        $manager->persist($user);
43
        $manager->flush();
44
45
        $clientManager = $this->getClientManager();
46
47
        /** @var Client $client */
48
        $client = $clientManager->createClient();
49
        $client->setRandomId('demo_client');
50
        $client->setSecret('secret_demo_client');
51
        $client->setAllowedGrantTypes(
52
            [
53
                OAuth2::GRANT_TYPE_USER_CREDENTIALS,
54
                OAuth2::GRANT_TYPE_IMPLICIT,
55
                OAuth2::GRANT_TYPE_REFRESH_TOKEN,
56
                OAuth2::GRANT_TYPE_AUTH_CODE,
57
                OAuth2::GRANT_TYPE_CLIENT_CREDENTIALS,
58
            ]
59
        );
60
        $clientManager->updateClient($client);
61
    }
62
63
    /**
64
     * @param string $email
65
     * @param string $password
66
     * @param bool   $enabled
67
     * @param array  $roles
68
     * @param string $currencyCode
69
     *
70
     * @return UserInterface
71
     */
72
    protected function createUser($email, $password, $enabled = true, array $roles = ['ROLE_USER'], $currencyCode = 'EUR')
73
    {
74
        $canonicalizer = $this->get('sylius.user.canonicalizer');
75
76
        /* @var $user UserInterface */
77
        $user = $this->getUserFactory()->createNew();
0 ignored issues
show
Documentation Bug introduced by
The method getUserFactory does not exist on object<Sylius\Bundle\Cor...xtures\ORM\LoadApiData>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
78
        $customer = $this->getCustomerFactory()->createNew();
0 ignored issues
show
Documentation Bug introduced by
The method getCustomerFactory does not exist on object<Sylius\Bundle\Cor...xtures\ORM\LoadApiData>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
        $customer->setFirstname($this->faker->firstName);
80
        $customer->setLastname($this->faker->lastName);
81
        $customer->setCurrencyCode($currencyCode);
82
        $user->setCustomer($customer);
83
        $user->setUsername($email);
84
        $user->setEmail($email);
85
        $user->setUsernameCanonical($canonicalizer->canonicalize($user->getUsername()));
86
        $user->setEmailCanonical($canonicalizer->canonicalize($user->getEmail()));
87
        $user->setPlainPassword($password);
88
        $user->setRoles($roles);
89
        $user->setEnabled($enabled);
90
91
        $this->get('sylius.user.password_updater')->updatePassword($user);
92
93
        return $user;
94
    }
95
96
    /**
97
     * @return ClientManagerInterface
98
     */
99
    private function getClientManager()
100
    {
101
        return $this->container->get('fos_oauth_server.client_manager.default');
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getOrder()
108
    {
109
        return 20;
110
    }
111
}
112