Issues (28)

src/Composer/DistDownloader.php (1 issue)

Severity
1
<?php
2
3
namespace App\Composer;
4
5
use App\Document\Package;
6
use App\Document\Provider;
7
use Composer\Downloader\FileDownloader;
8
use Composer\Package\Loader\ArrayLoader;
9
use Doctrine\ODM\MongoDB\DocumentManager;
10
11
class DistDownloader
12
{
13
    private $distPath;
14
    private $dm;
15
    private $factory;
16
17
    public function __construct(string $distPath, DocumentManager $dm, Factory $factory)
18
    {
19
        $this->distPath = $distPath;
20
        $this->dm = $dm;
21
        $this->factory = $factory;
22
    }
23
24
    public function download(string $name, string $version, string $reference, string $type): string
25
    {
26
        $path = $this->distPath.DIRECTORY_SEPARATOR.$name.DIRECTORY_SEPARATOR.$version;
27
        $filePath = $path.DIRECTORY_SEPARATOR.$reference;
28
29
        $config = $this->factory->createConfig();
30
        $io = new LogIO();
31
        $io->loadConfiguration($config);
32
33
        if (file_exists($filePath)) {
34
            return $filePath;
35
        }
36
37
        /** @var Provider $provider */
38
        $provider = $this->dm->getRepository('App:Provider')->find($name);
39
40
        if (!$provider) {
0 ignored issues
show
$provider is of type App\Document\Provider, thus it always evaluated to true.
Loading history...
41
            throw new \LogicException('Invalid provider');
42
        }
43
44
        /** @var Package $package */
45
        foreach ($provider->getPackages() as $package) {
46
            $data = $package->getData();
47
            if (
48
                isset($data['version_normalized']) && $data['version_normalized'] === $version &&
49
                isset($data['dist']['reference']) && $data['dist']['reference'] === $reference &&
50
                isset($data['dist']['type']) && $data['dist']['type'] === $type &&
51
                isset($data['dist']['url'])
52
            ) {
53
                $loader = new ArrayLoader();
54
                $composerPackage = $loader->load($data);
55
56
                $downloader = new FileDownloader($io, $config);
57
                $fileName = $downloader->download($composerPackage, $path, false);
58
59
                rename($fileName, $filePath);
60
61
                return $filePath;
62
            }
63
        }
64
65
        throw new \LogicException('Invalid dist');
66
    }
67
}
68