Plugin::initializeLinkedPackageFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the composer-link plugin.
7
 *
8
 * Copyright (c) 2021-2022 Sander Visser <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE.md
11
 * file that was distributed with this source code.
12
 *
13
 * @link https://github.com/SanderSander/composer-link
14
 */
15
16
namespace ComposerLink;
17
18
use Composer\Composer;
19
use Composer\EventDispatcher\EventSubscriberInterface;
20
use Composer\IO\IOInterface;
21
use Composer\Plugin\Capability\CommandProvider as ComposerCommandProvider;
22
use Composer\Plugin\Capable;
23
use Composer\Plugin\PluginInterface;
24
use Composer\Script\ScriptEvents;
25
use Composer\Util\Filesystem as ComposerFileSystem;
26
use ComposerLink\Actions\LinkPackages;
27
use ComposerLink\Repository\Repository;
28
use ComposerLink\Repository\RepositoryFactory;
29
use RuntimeException;
30
31
class Plugin implements PluginInterface, Capable, EventSubscriberInterface
32
{
33
    protected ?Repository $repository = null;
34
35
    protected ComposerFileSystem $filesystem;
36
37
    protected ?LinkManager $linkManager = null;
38
39
    protected ?LinkedPackageFactory $packageFactory = null;
40
41
    protected Composer $composer;
42
43
    protected ?LinkPackages $linkPackages;
44
45
    private ?RepositoryFactory $repositoryFactory;
46
47
    public function __construct(
48
        ComposerFileSystem $filesystem = null,
49
        LinkPackages $linkPackages = null,
50
        RepositoryFactory $repositoryFactory = null
51
    ) {
52
        $this->filesystem = $filesystem ?? new ComposerFileSystem();
53
        $this->linkPackages = $linkPackages;
54
        $this->repositoryFactory = $repositoryFactory;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function deactivate(Composer $composer, IOInterface $io)
61
    {
62
        $io->debug("[ComposerLink]\tPlugin is deactivated");
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function uninstall(Composer $composer, IOInterface $io)
69
    {
70
        // TODO remove repository file and restore all packages
71
        $io->debug("[ComposerLink]\tPlugin uninstalling");
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function activate(Composer $composer, IOInterface $io)
78
    {
79
        $io->debug("[ComposerLink]\tPlugin is activating");
80
        $this->composer = $composer;
81
82
        $this->initializeRepository();
83
        $this->initializeLinkedPackageFactory();
84
        $this->initializeLinkManager();
85
        $this->initializeLinkPackages();
86
    }
87
88
    protected function initializeRepository(): void
89
    {
90
        $storageFile = $this->composer->getConfig()
91
                ->get('vendor-dir') . DIRECTORY_SEPARATOR . 'linked-packages.json';
92
        if (is_null($this->repositoryFactory)) {
93
            $this->repositoryFactory = new RepositoryFactory();
94
        }
95
        $this->repository = $this->repositoryFactory->create($storageFile);
0 ignored issues
show
Bug introduced by
The method create() does not exist on null. ( Ignorable by Annotation )

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

95
        /** @scrutinizer ignore-call */ 
96
        $this->repository = $this->repositoryFactory->create($storageFile);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
    }
97
98
    protected function initializeLinkedPackageFactory(): void
99
    {
100
        $this->packageFactory = new LinkedPackageFactory(
101
            $this->composer->getInstallationManager(),
102
            $this->composer->getRepositoryManager()->getLocalRepository()
103
        );
104
    }
105
106
    protected function initializeLinkManager(): void
107
    {
108
        $this->linkManager = new LinkManager(
109
            $this->filesystem,
110
            $this->composer->getLoop(),
111
            $this->composer->getInstallationManager(),
112
            $this->composer->getRepositoryManager()->getLocalRepository()
113
        );
114
    }
115
116
    protected function initializeLinkPackages(): void
117
    {
118
        if (is_null($this->linkPackages)) {
119
            $this->linkPackages = new LinkPackages(
120
                $this->getLinkManager(),
121
                $this->getRepository(),
122
                $this->composer->getRepositoryManager()
123
            );
124
        }
125
    }
126
127
    public static function getSubscribedEvents(): array
128
    {
129
        return [
130
            ScriptEvents::POST_UPDATE_CMD => [
131
                ['linkLinkedPackages'],
132
            ],
133
        ];
134
    }
135
136
    public function getLinkManager(): LinkManager
137
    {
138
        if (is_null($this->linkManager)) {
139
            throw new RuntimeException('Plugin not activated');
140
        }
141
142
        return $this->linkManager;
143
    }
144
145
    public function linkLinkedPackages(): void
146
    {
147
        if (is_null($this->linkPackages)) {
148
            throw new RuntimeException('Plugin not activated');
149
        }
150
        $this->linkPackages->execute();
151
    }
152
153
    public function getCapabilities(): array
154
    {
155
        return [
156
            ComposerCommandProvider::class => CommandProvider::class,
157
        ];
158
    }
159
160
    public function getRepository(): Repository
161
    {
162
        if (is_null($this->repository)) {
163
            throw new RuntimeException('Plugin not activated');
164
        }
165
166
        return $this->repository;
167
    }
168
169
    public function getPackageFactory(): LinkedPackageFactory
170
    {
171
        if (is_null($this->packageFactory)) {
172
            throw new RuntimeException('Plugin not activated');
173
        }
174
175
        return $this->packageFactory;
176
    }
177
178
    /**
179
     * Check if this plugin is running from global or local project.
180
     */
181
    public function isGlobal(): bool
182
    {
183
        return getcwd() === $this->composer->getConfig()->get('home');
184
    }
185
}
186