Pickle::prepare()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 27
c 3
b 1
f 1
dl 0
loc 41
rs 8.8657
cc 6
nc 6
nop 0
1
<?php
2
3
/*
4
 * Pickle
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2015-2015, Pickle community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
namespace Pickle\Package\Convey\Command;
38
39
use Composer\Downloader\GitDownloader;
40
use Composer\Package\LinkConstraint\VersionConstraint;
41
use Composer\Package\Version\VersionParser;
42
use Exception;
43
use Pickle\Base\Abstracts;
44
use Pickle\Base\Interfaces;
45
use Pickle\Config;
46
use Pickle\Package;
47
48
class Pickle extends Abstracts\Package\Convey\Command implements Interfaces\Package\Convey\Command
49
{
50
    /**
51
     * @var string
52
     */
53
    protected $type;
54
55
    public function execute($target, $no_convert, $versionOverrideOverride)
56
    {
57
        $this->fetch($target);
58
59
        $exe = DefaultExecutor::factory($this);
60
61
        return $exe->execute($target, $no_convert, $versionOverrideOverride);
62
    }
63
64
    public function getType()
65
    {
66
        return Type::GIT;
67
    }
68
69
    protected function fetchPackageJson()
70
    {
71
        $extensionJson = @file_get_contents('http://localhost:8080/json/' . $this->name . '.json');
72
        if (!$extensionJson) {
73
            /** @var array $http_response_header */
74
            $status = $http_response_header[0] ?? '';
75
            if (strpos($status, '404') !== false) {
76
                throw new Exception("cannot find {$this->name}");
77
            }
78
            if ($status) {
79
                throw new Exception("http error while loading information for {$this->name}: " . $status);
80
            }
81
            throw new Exception("http error while loading information for {$this->name}: unknown error");
82
        }
83
84
        return json_decode($extensionJson, true);
85
    }
86
87
    protected function prepare()
88
    {
89
        $matches = null;
90
        if (Type::determinePickle($this->path, $matches) < 1) {
91
            throw new Exception('Not a pickle git URI');
92
        }
93
94
        $this->name = $matches['package'];
95
96
        $extension = $this->fetchPackageJson();
97
98
        $versionParser = new VersionParser();
99
        if ($matches['version'] == '') {
100
            $versions = array_keys($extension['packages'][$this->name]);
101
            if (count($versions) > 1) {
102
                $versionToUse = $versions[1];
103
            } else {
104
                $versionToUse = $versions[0];
105
            }
106
        } else {
107
            $versionConstraints = $versionParser->parseConstraints($matches['version']);
108
109
            /* versions are sorted decreasing */
110
            foreach (array_keys($extension['packages'][$this->name]) as $version) {
111
                $constraint = new VersionConstraint('=', $versionParser->normalize($version));
0 ignored issues
show
Deprecated Code introduced by
The class Composer\Package\LinkConstraint\VersionConstraint has been deprecated: use Composer\Semver\Constraint\Constraint instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

111
                $constraint = /** @scrutinizer ignore-deprecated */ new VersionConstraint('=', $versionParser->normalize($version));
Loading history...
112
                if ($versionConstraints->matches($constraint)) {
113
                    $versionToUse = $version;
114
                    break;
115
                }
116
            }
117
        }
118
119
        $package = $extension['packages'][$this->name][$versionToUse];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $versionToUse does not seem to be defined for all execution paths leading up to this point.
Loading history...
120
        $this->version = $versionToUse;
121
        $this->normalizedVersion = $versionParser->normalize($versionToUse);
0 ignored issues
show
Bug Best Practice introduced by
The property normalizedVersion does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
122
123
        $this->name = $matches['package'];
124
        $this->prettyVersion = $this->version;
125
        $this->url = $package['source']['url'];
126
        $this->reference = $package['source']['reference'];
0 ignored issues
show
Bug Best Practice introduced by
The property reference does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
127
        $this->type = $package['source']['type'];
128
    }
129
130
    protected function fetch($target)
131
    {
132
        $package = Package::factory($this->name, $this->version, $this->prettyVersion);
133
134
        $package->setSourceType($this->type);
135
        $package->setSourceUrl($this->url);
136
        $package->setSourceReference($this->version);
137
        $package->setRootDir($target);
138
139
        $downloader = new GitDownloader($this->io, new Config());
140
        if ($downloader !== null) {
141
            $downloader->download($package, $target);
142
        }
143
    }
144
}
145
146
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
147