Completed
Push — master ( b355e1...5b43bb )
by Allan
07:24
created

ClientsController::postClientAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
crap 6
1
<?php
2
3
namespace SMG\ManagerBundle\Controller;
4
5
use FOS\RestBundle\Controller\Annotations;
6
use FOS\RestBundle\Controller\FOSRestController;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
8
use SMG\OauthBundle\Entity\Client;
9
10
class ClientsController extends FOSRestController
11
{
12
    use Traits\TokenFromHeaderTrait;
13
14
    /**
15
     * List all oauth clients created.
16
     */
17
    public function getClientsAction()
18
    {
19
        $this->throwIfClientNot('backend');
20
        $clientManager = $this->get(
21
            'fos_oauth_server.client_manager.default'
22
        );
23
24
        $class = $clientManager->getClass();
25
26
        return $this->getDoctrine()->getRepository($class)->findAll();
27
    }
28
29
    /**
30
     * Create a new oauth client.
31
     *
32
     * @param Client $client Client posted by the caller
33
     *
34
     * @Annotations\Post("/clients")
35
     *
36
     * @ParamConverter(
37
     *     "client",
38
     *     converter="fos_rest.request_body"
39
     * )
40
     *
41
     * @return Client
42
     */
43
    public function postClientAction(Client $client)
44
    {
45
        $this->throwIfClientNot('backend');
46
47
        $clientManager = $this->get(
48
            'fos_oauth_server.client_manager.default'
49
        );
50
        $newClient = $clientManager->createClient();
51
52
        $newClient->setType($client->getType());
53
        $newClient->setMeta($client->getMeta());
54
        if ($client->getAllowedGrantTypes() !== null) {
55
            $newClient->setAllowedGrantTypes($client->getAllowedGrantTypes());
56
        }
57
58
        $clientManager->updateClient($newClient);
59
60
        return $newClient;
61
    }
62
}
63