Completed
Pull Request — develop (#575)
by
unknown
102:40 queued 37:52
created

ConfigService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getJsonFromFile() 0 12 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: taachja1
5
 * Date: 04.04.17
6
 * Time: 09:50
7
 */
8
namespace Graviton\ApiBundle\Service;
9
10
use Graviton\JsonSchemaBundle\Validator\InvalidJsonException;
11
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
12
13
/**
14
 * Class ConfigService
15
 * To provide a unique entry for Configuration data and caching
16
 *
17
 * @package Graviton\ApiBundle\Service
18
 */
19
class ConfigService
20
{
21
    /** @var string Where services are located */
22
    protected $dirService;
23
24
    /**
25
     * TODO Build save to cache for schema trees and so.
26
     * @var string Where we will cache definitions trees */
27
    protected $dirCache;
28
29
    public function __construct(
30
        $serviceDir,
31
        $cacheDir
32
    ) {
33
        $this->dirService = $serviceDir;
34
        if (strpos($this->dirService, 'vendor/graviton/graviton') ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
35
            $this->dirService = str_replace('vendor/graviton/graviton/', '', $this->dirService);
36
        }
37
        $this->dirCache = $cacheDir;
38
    }
39
40
    public function getJsonFromFile($fileName)
41
    {
42
        $string = file_get_contents($this->dirService . DIRECTORY_SEPARATOR . $fileName);
43
        if (!$string) {
44
            throw new ServiceNotFoundException('Service not found');
45
        }
46
        $json = json_decode($string);
47
        if (json_last_error()) {
48
            throw new InvalidJsonException('Service error, '.json_last_error_msg());
49
        }
50
        return $json;
51
    }
52
53
}