Failed Conditions
Pull Request — master (#47)
by Mateusz
09:48
created

CacheFileConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 61
ccs 0
cts 33
cp 0
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
15
use Puli\Manager\Api\Cache\CacheFile;
16
use Puli\Manager\Api\Module\InstallInfo;
17
use Puli\Manager\Assert\Assert;
18
use stdClass;
19
use Webmozart\Json\Conversion\JsonConverter;
20
21
/**
22
 * Converts cache file from and to json.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Mateusz Sojda <[email protected]>
27
 */
28
class CacheFileConverter implements JsonConverter
29
{
30
    /**
31
     * @var JsonConverter
32
     */
33
    private $moduleFileConverter;
34
35
    /**
36
     * Creates a new cache file converter.
37
     *
38
     * @param JsonConverter  $moduleFileConverter  The module file converter.
39
     */
40
    public function __construct(JsonConverter $moduleFileConverter)
41
    {
42
        $this->moduleFileConverter = $moduleFileConverter;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function toJson($cacheFile, array $options = array())
49
    {
50
        Assert::isInstanceOf($cacheFile, 'Puli\Manager\Api\Cache\CacheFile');
51
52
        $jsonData = new stdClass();
53
        $jsonData->modules = array();
54
        $jsonData->installInfo = array();
55
56
        foreach ($cacheFile->getModuleFiles() as $moduleFile) {
57
            $jsonData->modules[] = $this->moduleFileConverter->toJson($moduleFile, array(
58
                'targetVersion' => $moduleFile->getVersion(),
59
            ));
60
        }
61
        foreach ($cacheFile->getInstallInfos() as $installInfo) {
62
            $jsonData->installInfo[$installInfo->getModuleName()] = $installInfo->getInstallPath();
63
        }
64
65
        return $jsonData;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function fromJson($jsonData, array $options = array())
72
    {
73
        Assert::isInstanceOf($jsonData, 'stdClass');
74
75
        $cacheFile = new CacheFile(isset($options['path']) ? $options['path'] : null);
76
77
        foreach ($jsonData->modules as $module) {
78
            $moduleFile = $this->moduleFileConverter->fromJson($module);
79
            $cacheFile->addModuleFile($moduleFile);
80
        }
81
82
        foreach ($jsonData->installInfo as $packageName => $installPath) {
83
            $cacheFile->addInstallInfo(new InstallInfo($packageName, $installPath));
84
        }
85
86
        return $cacheFile;
87
    }
88
}