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

ConfigService::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
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
}