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

AbstractService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 22.97 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 82.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 17
loc 74
ccs 19
cts 23
cp 0.8261
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
B getImplementedMethods() 0 23 5
A getMethod() 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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