Completed
Pull Request — master (#47)
by Mateusz
07:52 queued 04:50
created

CacheFileConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 61
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4
ccs 23
cts 23
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toJson() 0 19 3
A fromJson() 0 17 4
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 20
    public function __construct(JsonConverter $moduleFileConverter)
40
    {
41 20
        $this->moduleFileConverter = $moduleFileConverter;
42 20
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 17
    public function toJson($cacheFile, array $options = array())
48
    {
49 17
        Assert::isInstanceOf($cacheFile, 'Puli\Manager\Api\Cache\CacheFile');
50
51 17
        $jsonData = new stdClass();
52 17
        $jsonData->modules = array();
53 17
        $jsonData->installInfos = array();
54
55 17
        foreach ($cacheFile->getModuleFiles() as $moduleFile) {
56 16
            $jsonData->modules[] = $this->moduleFileConverter->toJson($moduleFile, array(
57 16
                'targetVersion' => $moduleFile->getVersion(),
58
            ));
59
        }
60 17
        foreach ($cacheFile->getInstallInfos() as $installInfo) {
61 16
            $jsonData->installInfos[$installInfo->getModuleName()] = $installInfo->getInstallPath();
62
        }
63
64 17
        return $jsonData;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 18
    public function fromJson($jsonData, array $options = array())
71
    {
72 18
        Assert::isInstanceOf($jsonData, 'stdClass');
73
74 18
        $cacheFile = new CacheFile(isset($options['path']) ? $options['path'] : null);
75
76 18
        foreach ($jsonData->modules as $module) {
77 16
            $moduleFile = $this->moduleFileConverter->fromJson($module);
78 16
            $cacheFile->addModuleFile($moduleFile);
79
        }
80
81 18
        foreach ($jsonData->installInfos as $packageName => $installPath) {
82 16
            $cacheFile->addInstallInfo(new InstallInfo($packageName, $installPath));
83
        }
84
85 18
        return $cacheFile;
86
    }
87
}
88