SharedPackageDataManager::updatePackageUsageFile()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the "Composer Shared Package Plugin" package.
5
 *
6
 * https://github.com/Letudiant/composer-shared-package-plugin
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LEtudiant\Composer\Data\Package;
13
14
use Composer\Composer;
15
use Composer\Package\PackageInterface;
16
17
/**
18
 * @author Sylvain Lorinet <[email protected]>
19
 */
20
class SharedPackageDataManager implements PackageDataManagerInterface
21
{
22
    const PACKAGE_DATA_FILENAME       = 'packages.json';
23
    const PACKAGE_INSTALLATION_SOURCE = 'source';
24
25
    /**
26
     * @var Composer
27
     */
28
    protected $composer;
29
    /**
30
     * @var string
31
     */
32
    protected $vendorDir;
33
34
    /**
35
     * @var array
36
     */
37
    protected $packagesData;
38
39
40
    /**
41
     * @param Composer $composer
42
     */
43
    public function __construct(Composer $composer)
44
    {
45
        $this->composer = $composer;
46
    }
47
48
    /**
49
     * @param string $vendorDir
50
     */
51
    public function setVendorDir($vendorDir)
52
    {
53
        $this->vendorDir = $vendorDir;
54
    }
55
56
    /**
57
     * @param PackageInterface $package
58
     * @param array            $packageData
59
     */
60
    protected function updatePackageUsageFile(PackageInterface $package, array $packageData)
61
    {
62
        $packageKey = $package->getPrettyName() . '/' . $package->getPrettyVersion();
63
64
        // Remove the row if there is no data anymore
65
        if (!isset($packageData[0])) {
66
            if (isset($this->packagesData[$packageKey])) {
67
                unset($this->packagesData[$packageKey]);
68
            }
69
        } elseif (!isset($this->packagesData[$packageKey])) {
70
            $this->packagesData[$packageKey] = array(
71
                // Force "source" installation to ensure that we download VCS files
72
                'project-usage' => $packageData
73
            );
74
        } else {
75
            $this->packagesData[$packageKey]['project-usage'] = $packageData;
76
        }
77
78
        file_put_contents(
79
            $this->vendorDir . DIRECTORY_SEPARATOR . self::PACKAGE_DATA_FILENAME,
80
            json_encode($this->packagesData)
81
        );
82
    }
83
84
    /**
85
     * Add a row in the "packages.json" file, with the project name for the "package/version" key
86
     *
87
     * @param PackageInterface $package
88
     */
89
    public function addPackageUsage(PackageInterface $package)
90
    {
91
        $usageData = $this->getPackageUsage($package);
92
        $packageName = $this->composer->getPackage()->getName();
93
94
        if (!in_array($packageName, $usageData)) {
95
            $usageData[] = $packageName;
96
        }
97
98
        $this->updatePackageUsageFile($package, $usageData);
99
    }
100
101
    /**
102
     * Remove the row in the "packages.json" file
103
     *
104
     * @param PackageInterface $package
105
     */
106
    public function removePackageUsage(PackageInterface $package)
107
    {
108
        $usageData = $this->getPackageUsage($package);
109
        $newUsageData = array();
110
        $projectName = $this->composer->getPackage()->getName();
111
112
        foreach ($usageData as $usage) {
113
            if ($projectName !== $usage) {
114
                $newUsageData[] = $usage;
115
            }
116
        }
117
118
        $this->updatePackageUsageFile($package, $newUsageData);
119
    }
120
121
    /**
122
     * Return usage of the current package
123
     *
124
     * @param PackageInterface $package
125
     *
126
     * @return array
127
     */
128
    public function getPackageUsage(PackageInterface $package)
129
    {
130
        return $this->getPackageDataKey($package, 'project-usage', array());
131
    }
132
133
    /**
134
     * Initialize the package data array if not set
135
     */
136
    protected function initializePackageData()
137
    {
138
        $filePath = $this->vendorDir . DIRECTORY_SEPARATOR . self::PACKAGE_DATA_FILENAME;
139
        if (!is_file($filePath)) {
140
            $this->packagesData = array();
141
        } else {
142
            $this->packagesData = json_decode(file_get_contents($filePath), true);
143
        }
144
    }
145
146
    /**
147
     * @param PackageInterface $package
148
     * @param string           $key
149
     * @param mixed            $defaultValue
150
     *
151
     * @return mixed
152
     */
153
    protected function getPackageDataKey(PackageInterface $package, $key, $defaultValue = null)
154
    {
155
        if (!isset($this->packagesData)) {
156
            $this->initializePackageData();
157
        }
158
159
        $packageKey = $package->getPrettyName() . '/' . $package->getPrettyVersion();
160
        if (!isset($this->packagesData[$packageKey]) || !isset($this->packagesData[$packageKey][$key])) {
161
            return $defaultValue;
162
        }
163
164
        return $this->packagesData[$packageKey][$key];
165
    }
166
167
    /**
168
     * @param PackageInterface $package
169
     */
170
    public function setPackageInstallationSource(PackageInterface $package)
171
    {
172
        $package->setInstallationSource(static::PACKAGE_INSTALLATION_SOURCE);
173
    }
174
}
175