ControllerFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
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