Failed Conditions
Pull Request — master (#47)
by Mateusz
24:21
created

CacheJsonSerializer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 28.41 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 11
c 4
b 0
f 1
lcom 1
cbo 7
dl 25
loc 88
ccs 0
cts 55
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serializeCacheFile() 0 18 3
A unserializeCacheFile() 0 18 3
A encode() 9 9 1
A decode() 16 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\InvalidConfigException;
15
use Puli\Manager\Api\Package\InstallInfo;
16
use Puli\Manager\Api\Package\PackageFile;
17
use stdClass;
18
use Puli\Manager\Api\Cache\CacheFile;
19
use Puli\Manager\Api\Cache\CacheFileSerializer;
20
use Puli\Manager\Package\PackageJsonSerializer;
21
use Webmozart\Json\DecodingFailedException;
22
use Webmozart\Json\JsonDecoder;
23
use Webmozart\Json\JsonEncoder;
24
25
/**
26
 * Serializes and unserializes cache files to/from JSON.
27
 *
28
 * @since  1.0
29
 *
30
 * @author Mateusz Sojda <[email protected]>
31
 */
32
class CacheJsonSerializer implements CacheFileSerializer
33
{
34
    /**
35
     * @var PackageJsonSerializer
36
     */
37
    private $packageFileSerializer;
38
39
    /**
40
     * Creates new serializer.
41
     *
42
     * @param PackageJsonSerializer $packageFileSerializer The package files serializer.
43
     */
44
    public function __construct(PackageJsonSerializer $packageFileSerializer)
45
    {
46
        $this->packageFileSerializer = $packageFileSerializer;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function serializeCacheFile(CacheFile $cacheFile)
53
    {
54
        $jsonData = new stdClass();
55
        $jsonData->packages = array();
56
        $jsonData->installInfo = array();
57
58
        foreach ($cacheFile->getPackageFiles() as $packageFile) {
59
            $packageJsonData = new stdClass();
60
            $this->packageFileSerializer->packageFileToJson($packageFile, $packageJsonData);
61
            $jsonData->packages[] = $packageJsonData;
62
        }
63
64
        foreach ($cacheFile->getAllInstallInfo() as $installInfo) {
0 ignored issues
show
Bug introduced by
The method getAllInstallInfo() does not exist on Puli\Manager\Api\Cache\CacheFile. Did you maybe mean getInstallInfo()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
65
            $jsonData->installInfo[$installInfo->getPackageName()] = $installInfo->getInstallPath();
66
        }
67
68
        return $this->encode($jsonData);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function unserializeCacheFile($serialized, $path = null)
75
    {
76
        $cacheFile = new CacheFile($path);
77
78
        $jsonData = $this->decode($serialized, $path);
79
80
        foreach ($jsonData->packages as $package) {
81
            $packageFile = new PackageFile();
82
            $this->packageFileSerializer->jsonToPackageFile($package, $packageFile);
83
            $cacheFile->addPackageFile($packageFile);
84
        }
85
86
        foreach ($jsonData->installInfo as $packageName => $installPath) {
87
            $cacheFile->addInstallInfo(new InstallInfo($packageName, $installPath));
88
        }
89
90
        return $cacheFile;
91
    }
92
93 View Code Duplication
    private function encode(stdClass $jsonData)
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...
94
    {
95
        $encoder = new JsonEncoder();
96
        $encoder->setPrettyPrinting(true);
97
        $encoder->setEscapeSlash(false);
98
        $encoder->setTerminateWithLineFeed(true);
99
100
        return $encoder->encode($jsonData);
101
    }
102
103 View Code Duplication
    private function decode($json, $path = null)
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...
104
    {
105
        $decoder = new JsonDecoder();
106
107
        try {
108
            $jsonData = $decoder->decode($json);
109
        } catch (DecodingFailedException $e) {
110
            throw new InvalidConfigException(sprintf(
111
                "The configuration%s could not be decoded:\n%s",
112
                $path ? ' in '.$path : '',
113
                $e->getMessage()
114
            ), $e->getCode(), $e);
115
        }
116
117
        return $jsonData;
118
    }
119
}