FactoryParser::checkFactory()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 20
Ratio 83.33 %

Code Coverage

Tests 4
CRAP Score 7.2349

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 24
ccs 4
cts 18
cp 0.2222
rs 8.9713
cc 3
eloc 15
nc 3
nop 3
crap 7.2349
1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fidry\LaravelYaml\FileLoader\Parser\Yaml\Util;
13
14
use Fidry\LaravelYaml\DependencyInjection\Definition\FactoryInterface;
15
use Fidry\LaravelYaml\DependencyInjection\Definition\Factory;
16
use Fidry\LaravelYaml\DependencyInjection\Definition\ServiceInterface;
17
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
18
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ResolverInterface;
19
20
/**
21
 * @author Théo FIDRY <[email protected]>
22
 */
23
final class FactoryParser
24
{
25
    /**
26
     * @var ResolverInterface
27
     */
28
    private $serviceResolver;
29
30 44
    public function __construct(ResolverInterface $serviceResolver)
31
    {
32 44
        $this->serviceResolver = $serviceResolver;
33 44
    }
34
35
    /**
36
     * Parses a factory service definition and return the factory object.
37
     *
38
     * @param ServiceInterface $service
39
     * @param mixed            $factory
40
     * @param string           $fileName file name
41
     *
42
     * @return ServiceInterface
43
     * @throws InvalidArgumentException
44
     */
45 2
    public function parse(ServiceInterface $service, $factory, $fileName)
46
    {
47 2
        $this->checkFactory($service, $factory, $fileName);
48
49 2
        return $this->parseFactory($service, $factory[0], $factory[1], $fileName);
50
    }
51
52
    /**
53
     * @param ServiceInterface $service
54
     * @param mixed            $factory
55
     * @param string           $fileName file name
56
     *
57
     * @throws InvalidArgumentException
58
     */
59 2
    private function checkFactory(ServiceInterface $service, $factory, $fileName)
60
    {
61 2 View Code Duplication
        if (false === is_array($factory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
            throw new InvalidArgumentException(
63
                sprintf(
64
                    'Parameter "factory" must be an array for service "%s", but found "%s" instead in %s. Check your YAML syntax.',
65
                    $service->getName(),
66
                    gettype($factory),
67
                    $fileName
68
                )
69
            );
70
        }
71
72 2 View Code Duplication
        if (2 !== count($factory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
            throw new InvalidArgumentException(
74
                sprintf(
75
                    'Parameter "factory" expects two parameters for service "%s", but found "%s" instead in %s. Check your YAML syntax.',
76
                    $service->getName(),
77
                    gettype($factory),
78
                    $fileName
79
                )
80
            );
81
        }
82 2
    }
83
84
    /**
85
     * @param ServiceInterface $service
86
     * @param mixed            $factoryClass
87
     * @param mixed            $factoryMethod
88
     * @param string           $fileName file name
89
     *
90
     * @return FactoryInterface
91
     * @throws InvalidArgumentException
92
     */
93 2
    private function parseFactory(ServiceInterface $service, $factoryClass, $factoryMethod, $fileName)
94
    {
95 2 View Code Duplication
        if (false === is_string($factoryClass)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
            throw new InvalidArgumentException(
97
                sprintf(
98
                    'The first parameter of "factory" for service "%s" must be a class name or a reference to a service, but found "%s" instead in %s. Check your YAML syntax.',
99
                    $service->getName(),
100
                    gettype($factoryClass),
101
                    $fileName
102
                )
103
            );
104
        }
105 2
        $factoryClass = $this->serviceResolver->resolve($factoryClass);
106
107 2 View Code Duplication
        if (false === is_string($factoryMethod)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
108
            throw new InvalidArgumentException(
109
                sprintf(
110
                    'The second parameter of "factory" for service "%s" must be a class name or a reference to a service, but found "%s" instead in %s. Check your YAML syntax.',
111
                    $service->getName(),
112
                    gettype($factoryMethod),
113
                    $fileName
114
                )
115
            );
116
        }
117
118 2
        return new Factory($service, $factoryClass, $factoryMethod);
119
    }
120
}
121