Resources::getResource()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix;
14
15
use Apix\Entity,
16
    Apix\Entity\EntityInterface;
17
18
/**
19
 * Represents a collection of resources.
20
 */
21
class Resources
22
{
23
24
    /**
25
     * @var array
26
     */
27
    protected $resources = array();
28
29
    /**
30
     * @var EntityInterface
31
     */
32
    protected $entity = null;
33
34
    /**
35
     * Sets an entity object.
36
     *
37
     * @param EntityInterface $entity An entity object
38
     */
39
    public function setEntity(EntityInterface $entity)
40
    {
41
        $this->entity = $entity;
42
    }
43
44
    /**
45
     * Gets the current entity object.
46
     *
47
     * @return EntityInterface
48
     */
49
    public function getEntity()
50
    {
51
        return $this->entity;
52
    }
53
54
    /**
55
     * Adds a resource entity.
56
     *
57
     * @param  string $name     A resource name
58
     * @param  array  $resource A resource definition array
0 ignored issues
show
Documentation introduced by
There is no parameter named $resource. Did you maybe mean $resources?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
59
     * @return Entity
60
     */
61
    public function add($name, array $resources)
62
    {
63
        switch(true):
64
65
            case isset($resources['action'])
66
                && $resources['action'] instanceof \Closure:
67
                $this->setEntity(
68
                    new Entity\EntityClosure()
69
                );
70
            break;
71
72
            case isset($resources['controller']):
73
            default:
74
                $this->setEntity(
75
                    new Entity\EntityClass()
76
                );
77
78
        endswitch;
79
80
        if (!isset($this->resources[$name])) {
81
            $entity = get_class($this->getEntity());
82
            $this->resources[$name] = new $entity(); //new Entity($group);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
        }
84
        $this->resources[$name]->append($resources);
85
86
        return $this->resources[$name];
87
    }
88
89
    /**
90
     * Checks wether a specified resource name exists.
91
     *
92
     * @param  string  $name The resource name to check
93
     * @return boolean
94
     */
95
    public function has($name)
96
    {
97
        return isset($this->resources[$name]);
98
    }
99
100
    /**
101
     * Returns all the resources.
102
     *
103
     * @return array The array of resources
104
     */
105
    public function toArray()
106
    {
107
        return $this->resources;
108
    }
109
110
    /**
111
     * Gets the specified resource entity.
112
     *
113
     * @param  string                 $name The resource name to retrieve.
114
     * @throws /DomainException       404
115
     * @return Entity/EntityInterface
0 ignored issues
show
Documentation introduced by
The doc-type Entity/EntityInterface could not be parsed: Unknown type name "Entity/EntityInterface" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
116
     */
117
    public function getResource($name)
118
    {
119
        if (isset($this->resources[$name])) {
120
            return $this->resources[$name];
121
        }
122
123
        throw new \DomainException(
124
            sprintf('Invalid resource entity specified (%s).', $name), 404
125
        );
126
    }
127
128
    /**
129
     * Gets the specified ressource entity from a route object.
130
     *
131
     * @param  Router                 $route  The resource route object.
132
     * @param  boolean                $follow Wether to handle the default actions.
133
     * @throws /DomainException       404
134
     * @return Entity/EntityInterface
0 ignored issues
show
Documentation introduced by
The doc-type Entity/EntityInterface could not be parsed: Unknown type name "Entity/EntityInterface" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
135
     */
136
    public function get(Router &$route, $follow=true)
137
    {
138
        $entity = $this->getResource(
139
            $route->getName()
140
        );
141
142
        // swap if aliased/redirected
143
        if ($redirect = $entity->getRedirect()) {
144
            $entity = $this->getResource($redirect);
145
        }
146
147
        // handles the default actions but do not override a local action definition.
148
        if ($follow) {
149
150
            $method = $route->getMethod();
151
152
            if ( $method == 'HEAD' && $entity->hasMethod('GET') ) {
153
                $route->setMethod('GET');
154
            }
155
156
            if (
157
                ( $redirect = $entity->getDefaultAction($method) )
158
                && !$entity->hasMethod($method)
159
            ) {
160
                $entity = $this->getResource($redirect);
161
                #$route->setParams(array('entity' => clone $entity));
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
162
            }
163
        }
164
165
        // set this entity route.
166
        $entity->setRoute($route);
167
168
        return $entity;
169
    }
170
171
}
172