Passed
Push — master ( b9ec10...5392bd )
by Adrian Florin
02:43
created

JsonContent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the NeedleProject\FileIo package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace NeedleProject\FileIo\Content;
9
10
use NeedleProject\FileIo\Exception\ContentException;
11
12
/**
13
 * Class JsonContent
14
 *
15
 * @package NeedleProject\FileIo\Content
16
 * @author Adrian Tilita <[email protected]>
17
 * @copyright 2017 Adrian Tilita
18
 * @license https://opensource.org/licenses/MIT MIT Licence
19
 */
20
class JsonContent implements ContentInterface
21
{
22
    /**
23
     * @var null|string
24
     */
25
    private $content = null;
26
27
    /**
28
     * Content constructor.
29
     *
30
     * @param string $content
31
     */
32 6
    public function __construct(string $content)
33
    {
34 6
        $this->content = $content;
35 6
    }
36
37
    /**
38
     * @return string
39
     */
40 1
    public function get(): string
41
    {
42 1
        return $this->content;
43
    }
44
45
    /**
46
     * Return the content as an array
47
     *
48
     * @return array
49
     * @throws \NeedleProject\FileIo\Exception\ContentException
50
     */
51 3 View Code Duplication
    public function getArray(): array
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...
52
    {
53 3
        $content = json_decode($this->content, true);
54 3
        if (json_last_error() !== JSON_ERROR_NONE) {
55 2
            throw new ContentException(
56 2
                sprintf("Could not decode content, got %s", json_last_error_msg())
57
            );
58
        }
59 1
        return $content;
60
    }
61
62
    /**
63
     * @return \stdClass
64
     * @throws \NeedleProject\FileIo\Exception\ContentException
65
     */
66 3 View Code Duplication
    public function getObject(): \stdClass
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...
67
    {
68 3
        $content = json_decode($this->content);
69 3
        if (json_last_error() !== JSON_ERROR_NONE) {
70 2
            throw new ContentException(
71 2
                sprintf("Could not decode content, got %s", json_last_error_msg())
72
            );
73
        }
74 1
        return $content;
75
    }
76
}
77