Completed
Pull Request — master (#47)
by Mateusz
05:43
created

CacheFileConverter::__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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Cache;
13
14
use Puli\Manager\Api\Cache\CacheFile;
15
use Puli\Manager\Api\Module\InstallInfo;
16
use Puli\Manager\Assert\Assert;
17
use stdClass;
18
use Webmozart\Json\Conversion\JsonConverter;
19
20
/**
21
 * Converts cache file from and to json.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Mateusz Sojda <[email protected]>
26
 */
27
class CacheFileConverter implements JsonConverter
28
{
29
    /**
30
     * @var JsonConverter
31
     */
32
    private $moduleFileConverter;
33
34
    /**
35
     * Creates a new cache file converter.
36
     *
37
     * @param JsonConverter $moduleFileConverter The module file converter
38
     */
39 5
    public function __construct(JsonConverter $moduleFileConverter)
40
    {
41 5
        $this->moduleFileConverter = $moduleFileConverter;
42 5
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function toJson($cacheFile, array $options = array())
48
    {
49 2
        Assert::isInstanceOf($cacheFile, 'Puli\Manager\Api\Cache\CacheFile');
50
51 2
        $jsonData = new stdClass();
52 2
        $jsonData->modules = array();
53 2
        $jsonData->installInfos = array();
54
55 2
        foreach ($cacheFile->getModuleFiles() as $moduleFile) {
56 1
            $jsonData->modules[] = $this->moduleFileConverter->toJson($moduleFile, array(
57 1
                'targetVersion' => $moduleFile->getVersion(),
58
            ));
59
        }
60 2
        foreach ($cacheFile->getInstallInfos() as $installInfo) {
61 1
            $jsonData->installInfos[$installInfo->getModuleName()] = $installInfo->getInstallPath();
62
        }
63
64 2
        return $jsonData;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function fromJson($jsonData, array $options = array())
71
    {
72 3
        Assert::isInstanceOf($jsonData, 'stdClass');
73
74 3
        $cacheFile = new CacheFile(isset($options['path']) ? $options['path'] : null);
75
76 3
        foreach ($jsonData->modules as $module) {
77 1
            $moduleFile = $this->moduleFileConverter->fromJson($module);
78 1
            $cacheFile->addModuleFile($moduleFile);
79
        }
80
81 3
        foreach ($jsonData->installInfos as $packageName => $installPath) {
82 1
            $cacheFile->addInstallInfo(new InstallInfo($packageName, $installPath));
83
        }
84
85 3
        return $cacheFile;
86
    }
87
}
88