Completed
Push — travis_trusty ( e78e49...6f0d6f )
by André
54:54 queued 34:46
created

ExpressionRouterRootResourceBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A buildRootResource() 0 18 2
1
<?php
2
3
/**
4
 * File containing the ExpressionRouterRootResourceBuilder class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Server\Service;
12
13
use eZ\Publish\Core\REST\Common\Values;
14
use eZ\Publish\Core\REST\Common\Values\Root;
15
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16
use Symfony\Component\Routing\RouterInterface;
17
18
/**
19
 * Class ExpressionRouterRootResourceBuilder.
20
 *
21
 * This class builds a Root from an array building the href's using ExpressionLanguage
22
 * to build href's from the router or templateRouter.
23
 *
24
 * Example $resourceConfig structure:
25
 *
26
 * array(
27
 *      'content' => array(
28
 *          'mediaType' => '',
29
 *          'href' => 'router.generate("ezpublish_rest_listContentTypes")',
30
 *      ),
31
 *      'usersByRoleId' => array(
32
 *          'mediaType' => 'UserRefList',
33
 *          'href' => 'templateRouter.generate("ezpublish_rest_loadUsers", {roleId: "{roleId}"})',
34
 *      ),
35
 * )
36
 */
37
class ExpressionRouterRootResourceBuilder implements RootResourceBuilderInterface
38
{
39
    /**
40
     * @var \Symfony\Component\Routing\RouterInterface
41
     */
42
    protected $router;
43
44
    /**
45
     * @var \Symfony\Component\Routing\RouterInterface
46
     */
47
    protected $templateRouter;
48
49
    /**
50
     * @var array
51
     */
52
    protected $resourceConfig;
53
54
    /**
55
     * Creates a new resource builder.
56
     *
57
     * @param \Symfony\Component\Routing\RouterInterface $router
58
     * @param \Symfony\Component\Routing\RouterInterface $templateRouter
59
     * @param array $resourceConfig
60
     */
61
    public function __construct(RouterInterface $router, RouterInterface $templateRouter, array $resourceConfig)
62
    {
63
        $this->router = $router;
64
        $this->templateRouter = $templateRouter;
65
        $this->resourceConfig = $resourceConfig;
66
    }
67
68
    /**
69
     * Build root resource.
70
     *
71
     * @return array|\eZ\Publish\Core\REST\Common\Values\Root
72
     */
73
    public function buildRootResource()
74
    {
75
        $language = new ExpressionLanguage();
76
77
        $resources = array();
78
        foreach ($this->resourceConfig as $name => $resource) {
79
            $resources[] = new Values\Resource(
80
                $name,
81
                $resource['mediaType'],
82
                $language->evaluate($resource['href'], [
83
                    'router' => $this->router,
84
                    'templateRouter' => $this->templateRouter,
85
                ])
86
            );
87
        }
88
89
        return new Root($resources);
90
    }
91
}
92