ControllerFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 10.77 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 7
loc 65
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A create() 0 15 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
2
declare(strict_types = 1);
3
4
namespace Zortje\MVC\Controller;
5
6
use Zortje\MVC\Configuration\Configuration;
7
use Zortje\MVC\Controller\Exception\ControllerInvalidSuperclassException;
8
use Zortje\MVC\Controller\Exception\ControllerNonexistentException;
9
use Zortje\MVC\Model\Table\Entity\Entity;
10
use Zortje\MVC\Network\Request;
11
12
/**
13
 * Class ControllerFactory
14
 *
15
 * @package Zortje\MVC\Controller
16
 */
17
class ControllerFactory
18
{
19
20
    /**
21
     * @var \PDO PDO
22
     */
23
    protected $pdo;
24
25
    /**
26
     * @var Configuration
27
     */
28
    protected $configuration;
29
30
    /**
31
     * @var Request
32
     */
33
    protected $request;
34
35
    /**
36
     * @var Entity|null User
37
     */
38
    protected $user;
39
40
    /**
41
     * ControllerFactory constructor.
42
     *
43
     * @param \PDO          $pdo
44
     * @param Configuration $configuration
45
     * @param Request       $request
46
     * @param Entity|null   $user
47
     */
48 1 View Code Duplication
    public function __construct(\PDO $pdo, Configuration $configuration, Request $request, Entity $user = null)
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...
49
    {
50 1
        $this->pdo           = $pdo;
51 1
        $this->configuration = $configuration;
52 1
        $this->request       = $request;
53 1
        $this->user          = $user;
54 1
    }
55
56
    /**
57
     * Initialize controller
58
     *
59
     * @param string $controller Controller class name
60
     *
61
     * @return Controller Controller object
62
     *
63
     * @throws ControllerInvalidSuperclassException
64
     * @throws ControllerNonexistentException
65
     */
66 3
    public function create(string $controller): Controller
67
    {
68 3
        if (!class_exists($controller)) {
69 1
            throw new ControllerNonexistentException([$controller]);
70 2
        } elseif (!is_subclass_of($controller, Controller::class)) {
71 1
            throw new ControllerInvalidSuperclassException([$controller]);
72
        }
73
74
        /**
75
         * @var Controller $controller
76
         */
77 1
        $controller = new $controller($this->pdo, $this->configuration, $this->request, $this->user);
78
79 1
        return $controller;
80
    }
81
}
82