Completed
Push — master ( ad7fd3...b10ab8 )
by Théo
03:40
created

BaseReferenceResolver::resolve()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 8.8571
cc 6
eloc 13
nc 6
nop 2
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\DependencyInjection\Resolver;
13
14
use Fidry\LaravelYaml\DependencyInjection\Definition\Reference;
15
use Fidry\LaravelYaml\Exception\DependencyInjection\Resolver\Exception;
16
use Fidry\LaravelYaml\Exception\ServiceNotFoundException;
17
use Illuminate\Contracts\Container\BindingResolutionException;
18
use Illuminate\Contracts\Foundation\Application;
19
20
/**
21
 * @author Théo FIDRY <[email protected]>
22
 */
23
final class BaseReferenceResolver implements ReferenceResolverInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function resolve(Reference $reference, Application $application)
29
    {
30
        try {
31
            return $application->make($reference->getId());
32
        } catch (BindingResolutionException $exception) {
33
            switch (true) {
34
                case $reference->throwExceptionOnInvalidBehaviour():
35
                    throw new ServiceNotFoundException(sprintf('Could not find service "%s"', $reference->getId()));
36
37
                case $reference->returnNullOnInvalidBehaviour():
38
                case $reference->ignoreOnInvalidBehaviour():
39
                default:
40
                    return null;
41
            }
42
        } catch (\Exception $exception) {
43
            throw new Exception(sprintf('Could not resolve service "%s"', $reference->getId()), 0, $exception);
44
        }
45
    }
46
}
47