Completed
Push — master ( 52b242...112fdc )
by Thomas
17s queued 11s
created

ActionMetadataFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\ActionMetadata;
15
16
use Ekino\Drupal\Debug\Action\ActionInterface;
17
use Ekino\Drupal\Debug\Action\ActionWithOptionsInterface;
18
use Ekino\Drupal\Debug\ActionMetadata\Model\ActionMetadata;
19
use Ekino\Drupal\Debug\ActionMetadata\Model\ActionWithOptionsMetadata;
20
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
21
22
class ActionMetadataFactory
23
{
24
    public function create(string $class): ActionMetadata
25
    {
26
        $refl = new \ReflectionClass($class);
27
28
        if (!$refl->implementsInterface(ActionInterface::class)) {
29
            throw new InvalidConfigurationException(\sprintf('The "%s" class should implement the "%s" interface.', $class, ActionInterface::class));
30
        }
31
32
        return $refl->implementsInterface(ActionWithOptionsInterface::class) ?
33
            new ActionWithOptionsMetadata($refl, $class, $refl->getMethod('getOptionsClass')->invoke(null)) :
34
            new ActionMetadata($refl, $class);
35
    }
36
}
37