JsonFileReader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
eloc 15
c 2
b 0
f 1
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A readToArray() 0 23 4
A __construct() 0 3 1
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Readers;
7
8
class JsonFileReader
9
{
10
    /**
11
     * @var \Vaimo\ComposerChangelogs\Decoders\JsonDecoder
12
     */
13
    private $jsonDecoder;
14
15
    public function __construct()
16
    {
17
        $this->jsonDecoder = new \Vaimo\ComposerChangelogs\Decoders\JsonDecoder();
18
    }
19
20
    public function readToArray($source)
21
    {
22
        if (!file_exists($source)) {
23
            throw new \Vaimo\ComposerChangelogs\Exceptions\ReaderException(
24
                sprintf('File not found: %s', $source)
25
            );
26
        }
27
28
        $sourceData = file_get_contents($source);
29
30
        try {
31
            $fileContents = $this->jsonDecoder->decode($sourceData);
32
        } catch (\Vaimo\ComposerChangelogs\Exceptions\DecoderException $exception) {
33
            $message = sprintf('Failed to retrieve contents of %s', $source);
34
35
            throw new \Vaimo\ComposerChangelogs\Exceptions\ReaderException($message, 0, $exception);
36
        }
37
38
        if (!is_array($fileContents)) {
39
            return array();
40
        }
41
        
42
        return $fileContents;
43
    }
44
}
45