Completed
Push — 4.0 ( bcbbe3...4201d3 )
by Marco
09:53
created

AbstractService::getMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
ccs 3
cts 6
cp 0.5
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 4.125
1
<?php namespace Comodojo\Dispatcher\Service;
2
3
use \Comodojo\Dispatcher\Components\AbstractModel;
4
use \Comodojo\Foundation\Base\Configuration;
5
use \Comodojo\Dispatcher\Request\Model as Request;
6
use \Comodojo\Dispatcher\Router\Model as Router;
7
use \Comodojo\Dispatcher\Response\Model as Response;
8
use \Comodojo\Dispatcher\Extra\Model as Extra;
9
use \Psr\Log\LoggerInterface;
10
use \Exception;
11
12
/**
13
 * @package     Comodojo Dispatcher
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @author      Marco Castiello <[email protected]>
16
 * @license     GPL-3.0+
17
 *
18
 * LICENSE:
19
 *
20
 * This program is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License as
22
 * published by the Free Software Foundation, either version 3 of the
23
 * License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU Affero General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU Affero General Public License
31
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32
 */
33
34
abstract class AbstractService extends AbstractModel {
35
36
    protected $mode = self::READONLY;
37
38 1 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
        Configuration $configuration,
40
        LoggerInterface $logger,
41
        Request $request,
42
        Router $router,
43
        Response $response,
44
        Extra $extra
45
    ) {
46
47 1
        parent::__construct($configuration, $logger);
48
49 1
        $this->setRaw('request', $request);
50 1
        $this->setRaw('router', $router);
51 1
        $this->setRaw('response', $response);
52 1
        $this->setRaw('extra', $extra);
53
54 1
    }
55
56
    /**
57
     * Get service-implemented HTTP methods
58
     *
59
     * @return  array   Service implemented methods, in uppercase
60
     */
61 2
    public function getImplementedMethods() {
62
63 2
        $supported_methods = $this->configuration->get('supported-http-methods');
0 ignored issues
show
Documentation introduced by
The property configuration does not exist on object<Comodojo\Dispatch...ervice\AbstractService>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64
65 2
        if ( is_null($supported_methods) ) $supported_methods = array('GET','PUT','POST','DELETE','OPTIONS','HEAD','TRACE','CONNECT','PURGE');
66
67 2
        if ( method_exists($this, 'any') ) {
68
69
            return $supported_methods;
70
71
        }
72
73 2
        $implemented_methods = array();
74
75 2
        foreach ( $supported_methods as $method ) {
76
77 2
            if ( method_exists($this, strtolower($method)) ) array_push($implemented_methods,$method);
78
79 2
        }
80
81 2
        return $implemented_methods;
82
83
    }
84
85
    /**
86
     * Return the callable class method that reflect the requested one
87
     *
88
     */
89 2
    public function getMethod($method) {
90
91 2
        if ( method_exists($this, strtolower($method)) ) {
92
93 2
            return strtolower($method);
94
95
        } else if ( method_exists($this, strtolower('any')) ) {
96
97
            return 'any';
98
99
        } else {
100
101
            return "any";
102
103
        }
104
105
    }
106
107
}
108