WrappedCommandTrait::resetComposer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[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
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\Task\Composer\WrappedCommand;
22
23
use Composer\Composer;
24
25
/**
26
 * This class provides a simple way to ad-hoc create a composer instance from a command via a factory closure.
27
 */
28
trait WrappedCommandTrait
29
{
30
    /**
31
     * The current composer instance.
32
     *
33
     * @var Composer
34
     */
35
    private $composer;
36
37
    /**
38
     * The factory method to use for creating a composer instance.
39
     *
40
     * @var \Closure
41
     */
42
    private $composerFactory;
43
44
    /**
45
     * Retrieve the composer instance.
46
     *
47
     * @param bool $required       Flag if the instance is required.
48
     *
49
     * @param bool $disablePlugins Flag if plugins shall get disabled.
50
     *
51
     * @return Composer|null
52
     *
53
     * @throws \RuntimeException When no factory closure has been set.
54
     */
55
    public function getComposer($required = true, $disablePlugins = false)
56
    {
57
        if (null === $this->composer) {
58
            if ($this->composerFactory) {
59
                $this->composer = call_user_func($this->composerFactory, $required, $disablePlugins);
60
            }
61
62
            if ($required && !$this->composer) {
63
                throw new \RuntimeException(
64
                    'You must define a factory closure for wrapped commands to retrieve the ' .
65
                    'composer instance.'
66
                );
67
            }
68
        }
69
70
        return $this->composer;
71
    }
72
73
    /**
74
     * Save the composer instance to use.
75
     *
76
     * @param Composer $composer The instance to use.
77
     *
78
     * @return void
79
     */
80
    public function setComposer(Composer $composer)
81
    {
82
        $this->composer = $composer;
83
    }
84
85
    /**
86
     * Removes the cached composer instance.
87
     *
88
     * @return void
89
     */
90
    public function resetComposer()
91
    {
92
        $this->composer = null;
93
    }
94
95
    /**
96
     * Set the composer factory closure.
97
     *
98
     * @param \Closure $composerFactory The new factory function.
99
     *
100
     * @return WrappedCommandTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type WrappedCommandTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
101
     */
102
    public function setComposerFactory($composerFactory)
103
    {
104
        $this->composerFactory = $composerFactory;
105
106
        return $this;
107
    }
108
}
109