Test Failed
Pull Request — master (#19)
by Flo
02:48
created

AbstractControllerServiceFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 23
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A createService() 0 13 3
1
<?php
2
/**
3
 * Class AbstractControllerServiceFactory | AbstractControllerServiceFactory.phptory.php
4
 * @package Faulancer\Service\Factory
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\Service\Factory;
8
9
use Faulancer\Http\Request;
10
use Faulancer\Service\AbstractControllerService;
11
use Faulancer\ServiceLocator\FactoryInterface;
12
use Faulancer\ServiceLocator\ServiceLocatorInterface;
13
14
/**
15
 * Class AbstractControllerServiceFactory
16
 * @codeCoverageIgnore
17
 */
18
class AbstractControllerServiceFactory implements FactoryInterface
19
{
20
21
    /**
22
     * @param ServiceLocatorInterface $serviceLocator
23
     * @return AbstractControllerService
24
     * @codeCoverageIgnore
25
     */
26
    public function createService(ServiceLocatorInterface $serviceLocator)
0 ignored issues
show
Coding Style introduced by
createService uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
27
    {
28
        $request = new Request();
29
30
        if (empty($_SERVER['REQUEST_URI']) && empty($_SERVER['REQUEST_METHOD'])) {
31
            $request->setMethod('GET');
32
            $request->setUri('/');
33
        } else {
34
            $request->createFromHeaders();
35
        }
36
37
        return new AbstractControllerService($request);
38
    }
39
40
}