Completed
Push — master ( 754379...caf5ef )
by Muhammad Kashif
23:36
created

JsonServiceFactory::loadJsonFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Gr8abbasi\Container\Factory;
4
5
use Gr8abbasi\Container\Exception\NotFoundException;
6
use Gr8abbasi\Container\Exception\ContainerException;
7
8
/**
9
 * Class JsonServiceFactory
10
 */
11
class JsonServiceFactory implements ServiceFactoryInterface
12
{
13
    /**
14
     * @var array $services
15
     */
16
    private $services;
17
18
    /**
19
     * @var array $serviceList
20
     */
21
    private $serviceList;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param string $jsonFile
27
     *
28
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30
    public function __construct($jsonFile)
31
    {
32
        $this->services = $this->loadServiceList($jsonFile);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->loadServiceList($jsonFile) of type null is incompatible with the declared type array of property $services.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Bug introduced by
Are you sure the assignment to $this->services is correct as $this->loadServiceList($jsonFile) (which targets Gr8abbasi\Container\Fact...tory::loadServiceList()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
33
    }
34
35
    /**
36
     * @param string $jsonFile
37
     *
38
     * @return array
39
     */
40
    private function loadJsonFile($jsonFile)
41
    {
42
        $jsonString = file_get_contents($jsonFile);
43
        return json_decode($jsonString);
44
    }
45
46
    /**
47
     * @param string $jsonFile
48
     *
49
     * @return void
50
     */
51
    private function loadServiceList($jsonFile)
52
    {
53
       $services =  $this->loadJsonFile($jsonFile);
54
55
       foreach($services->services as $service) {
56
           $this->serviceList[$service->id] = $service;
57
       }
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 View Code Duplication
    public function create($service)
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...
64
    {
65
        if(!isset($this->serviceList[$service])) {
66
            throw new NotFoundException('Service not found: ' . $service);
67
        }
68
69
        if (!class_exists($this->serviceList[$service]->class)) {
70
            throw new ContainerException(
71
                'Class does not exists: ' . $this->serviceList[$service]->class
72
            );
73
        }
74
75
        $service = new \ReflectionClass($this->serviceList[$service]->class);
76
        return $service->newInstance();
77
78
    }
79
}
80