Help   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 158
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onRead() 0 22 1
B onHelp() 0 39 6
A getResourcesDocs() 0 20 4
A getEntityDocs() 0 11 1
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\Resource;
14
15
use Apix\Entity,
16
    Apix\Server,
17
    Apix\Request,
18
    Apix\Router,
19
    Apix\View\ViewModel;
20
21
/**
22
 * Help
23
 * This resource entity provides in-line referencial of all the API resources.
24
 */
25
class Help
26
{
27
    // only use in verbose mode.
28
    public $private_nodeName = 'verbose';
29
30
    private $rel_path = '/help';
31
32
    /**
33
     * Help - Provides in-line referencial to this API.
34
     *
35
     * This resource entity provides documentation and in-line referencial to this API's resources.
36
     * By specify a resource and method you can narrow down to specific section.
37
     *
38
     * @param  string $resource Optional. A string of characters used to identify
39
     *                                    a specific resource entity.
40
     *                                    e.g. /help/resource
41
     * @param  array  $filters  Optional. Some filters to narrow down the resultset.
42
     * @global string $method    Default "GET". The resource HTTP method to interact.
43
     * @return array  An array documentating either all the available resources
44
     *                or if provided, the specified <b>:resource</b>.
45
     *
46
     * @example <p><pre>apixs:
47
     *    help:
48
     *       title: Help - Provides in-line referencial to this API.
49
     *       description: This resource entity provides documentation and ...
50
     * </pre></p>
51
     * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.2
52
     */
53
    public function onRead($resource=null, array $filters=null)
0 ignored issues
show
Unused Code introduced by
The parameter $resource 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...
54
    {
55
        $server = \Apix\Service::get('server');
56
        $resource = preg_replace(
57
            '@^(.*?)' . preg_quote($this->rel_path) . '(\.\w+)?@',
58
            '',
59
            $server->request->getUri()
60
        );
61
62
        // $resource = rawurldecode($resource);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
63
        // if (!$server->resources->has($resource)) {
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...
64
        //     return array();
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...
65
        // }
66
67
        // if (
68
        //     !empty($resource)
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
69
        //     && $server->resources->has($resource)
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
70
        // ) {
71
            $server->getRoute()->setName($resource);
72
        // }
73
        return $this->onHelp($server, $filters);
74
    }
75
76
    /**
77
     * Outputs info for a resource entity.
78
     *
79
     * The OPTIONS method represents a request for information about the
80
     * communication options available on the request/response chain
81
     * identified by the Request-URI. This method allows the client to determine
82
     * the options and/or requirements associated with a resource,
83
     * or the capabilities of a server, without implying a resource action or
84
     * initiating a resource retrieval.
85
     *
86
     *
87
     * @param  Server $server  The main server object.
88
     * @param  array  $filters An array of filters.
89
     * @return array  An array of documentation data.
90
     *
91
     * @api_link    OPTIONS /path/to/entity
92
     * @api_link    OPTIONS /*
93
     * @apix_man_toc_hidden
94
     */
95
    public function onHelp(Server $server, array $filters=null)
0 ignored issues
show
Unused Code introduced by
The parameter $filters 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...
96
    {
97
        $route = $server->getRoute();
98
99
        // insures the top node is set to help.
100
        $route->setController('help');
101
102
        $entity = $route->getName() != '/' && $route->getName() != '/*'
103
            && $route->getPath() != '/help'
104
            ? $server->resources->get($route, false)
105
            : null;
106
107
        // TOC of all entities.
108
        if (null === $entity) {
109
110
            $docs = array(
111
                'items' => self::getResourcesDocs($server)
112
            );
113
114
            // // set Content-Type (negotiate or default)
115
            // if(
116
            //      $request->hasHeader('CONTENT_LENGTH')
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...
117
            //      || $request->hasHeader('TRANSFER_ENCODING')
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
118
            // ) {
119
            //     // @expect   client request has an entity-body (indicated by Content-Length or Transfer-Encoding) then client's Content-Type must be set.
120
            //     return array('doc'=>'Todo: return all the resource doc as per CONTENT_LENGTH and/or TRANSFER_ENCODING');
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% 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...
121
            // }
122
123
        // Manual for the specified entity doc.
124
        } else {
125
            $docs = self::getEntityDocs(
126
                $entity,
127
                $server->request->getParam('method') ?: 'GET',
128
                $route->getName()
129
            );
130
        }
131
132
        return $docs;
133
    }
134
135
    /**
136
     * Get the documentation of all the resource entities.
137
     *
138
     * @param  Server $server
139
     * @return array  The documentation for all resource entities.
140
     */
141
    public static function getResourcesDocs(Server $server)
142
    {
143
        $docs = array();
144
        $redir = array();
145
        $resources = $server->resources->toArray();
146
        ksort($resources);
147
        foreach ($resources as $path => $entity) {
148
            if (!$entity->hasRedirect()) {
149
                $item = self::getEntityDocs($entity, null, $path);
150
                $item['path'] = isset($redir[$path])
151
                                    ? $redir[$path]
152
                                    : $path;
153
                $docs[] = $item;
154
            } else {
155
                $redir[$entity->getRedirect()] = $path;
156
            }
157
        }
158
159
        return $docs;
160
    }
161
162
    /**
163
     * Get the documentation for the provided entity and method.
164
     *
165
     * @param  EntityInterface $entity The Entity object to interact with.
166
     * @param  string|null     $method Optional. The Request-method or all.
167
     * @param  string|null     $path   Optional. Request-URI for that entity.
168
     * @return array           The entity array documentation.
169
     */
170
    public static function getEntityDocs(Entity $entity, $method=null, $path=null)
171
    {
172
        $docs = $entity->getDocs($method);
173
        // $docs = (null !== $method || $entity->hasMethod($method))
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
174
        //     ? $entity->getDocs($method) // get the specified doc method.
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
175
        //     : $entity->getDocs();       // get all the docs for all methods
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
176
177
        $docs['path'] = $path;
178
179
        return $docs;
180
    }
181
182
}
183