Completed
Push — feature/middleware ( 17908d...975e2b )
by Romain
02:04
created

getOptionsClassNameFromProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Middleware;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Exceptions\ClassNotFoundException;
18
use Romm\Formz\Exceptions\InvalidArgumentTypeException;
19
use Romm\Formz\Middleware\Option\OptionDefinitionInterface;
20
use Romm\Formz\Service\Traits\ExtendedSelfInstantiateTrait;
21
use TYPO3\CMS\Core\SingletonInterface;
22
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
23
24
class MiddlewareFactory implements SingletonInterface
25
{
26
    use ExtendedSelfInstantiateTrait;
27
28
    /**
29
     * @var ReflectionService
30
     */
31
    protected $reflectionService;
32
33
    /**
34
     * @param ReflectionService $reflectionService
35
     */
36
    public function __construct(ReflectionService $reflectionService)
37
    {
38
        $this->reflectionService = $reflectionService;
39
    }
40
41
    /**
42
     * @param string   $className
43
     * @param callable $optionsCallback
44
     * @return MiddlewareComponentInterface
45
     * @throws ClassNotFoundException
46
     * @throws InvalidArgumentTypeException
47
     */
48
    public function create($className, callable $optionsCallback = null)
49
    {
50
        if (false === class_exists($className)) {
51
            throw ClassNotFoundException::middlewareClassNameNotFound($className);
52
        }
53
54
        if (false === in_array(MiddlewareComponentInterface::class, class_implements($className))) {
55
            throw InvalidArgumentTypeException::middlewareWrongClassName($className);
56
        }
57
58
        /** @var MiddlewareComponentInterface $className */
59
        $optionsType = $className::getOptionsClassName();
60
        $options = Core::instantiate($optionsType);
61
62
        if (is_callable($optionsCallback)) {
63
            call_user_func($optionsCallback, $options);
64
        }
65
66
        /** @var MiddlewareComponentInterface $middleware */
67
        $middleware = Core::instantiate($className, $options);
68
69
        return $middleware;
70
    }
71
72
    /**
73
     * Returns the option class name for the given middleware, depending on the
74
     * `@var` annotation of the `$options` property.
75
     *
76
     * @param string $middlewareClassName
77
     * @return string
78
     * @throws ClassNotFoundException
79
     * @throws InvalidArgumentTypeException
80
     */
81
    public function getOptionsClassNameFromProperty($middlewareClassName)
82
    {
83
        $property = $this->reflectionService->getClassSchema($middlewareClassName)->getProperty('options');
84
        $optionsType = $property['type'];
85
86
        if (false === class_exists($optionsType)) {
87
            throw ClassNotFoundException::middlewareOptionsPropertyClassNameNotFound($middlewareClassName, $optionsType);
88
        }
89
90
        if (false === in_array(OptionDefinitionInterface::class, class_implements($optionsType))) {
91
            throw InvalidArgumentTypeException::middlewareOptionPropertyWrongClassName($optionsType);
92
        }
93
94
        return $optionsType;
95
    }
96
}
97