Issues (718)

src/Charcoal/Admin/AdminScript.php (3 issues)

1
<?php
2
3
namespace Charcoal\Admin;
4
5
// From Pimple
6
use Pimple\Container;
7
8
// From 'league/climate'
9
use League\CLImate\TerminalObject\Dynamic\Input as LeagueInput;
10
11
// From 'charcoal-factory'
12
use Charcoal\Factory\FactoryInterface;
13
14
// From 'charcoal-app'
15
use Charcoal\App\Script\AbstractScript;
16
17
// From 'charcoal-property'
18
use Charcoal\Property\PropertyInterface;
19
20
// From 'charcoal-translator'
21
use Charcoal\Translator\TranslatorAwareTrait;
22
23
// From 'charcoal-admin'
24
use Charcoal\Admin\Support\BaseUrlTrait;
25
26
/**
27
 *
28
 */
29
abstract class AdminScript extends AbstractScript
30
{
31
    use BaseUrlTrait;
32
    use TranslatorAwareTrait;
33
34
    /**
35
     * The model factory.
36
     *
37
     * @var FactoryInterface
38
     */
39
    private $modelFactory;
40
41
    /**
42
     * @param  Container $container Pimple DI container.
43
     * @return void
44
     */
45
    protected function setDependencies(Container $container)
46
    {
47
        parent::setDependencies($container);
48
49
        // Satisfies TranslatorAwareTrait dependencies
50
        $this->setTranslator($container['translator']);
51
52
        // Satisfies BaseUrlTrait dependencies
53
        $this->setBaseUrl($container['base-url']);
54
        $this->setAdminUrl($container['admin/base-url']);
55
56
        // Satisfies AdminScript dependencies
57
        $this->setModelFactory($container['model/factory']);
58
    }
59
60
    /**
61
     * @param  PropertyInterface $prop  The property to retrieve input from.
62
     * @param  string|null       $label Optional. The input label.
63
     * @return LeagueInput The League's terminal input object.
64
     */
65
    protected function propertyToInput(PropertyInterface $prop, $label = null)
66
    {
67
        $climate = $this->climate();
68
69
        if ($label === null) {
70
            $label = sprintf(
71
                'Enter value for "%s":',
72
                $prop->label()
73
            );
74
        }
75
76
        if ($prop->type() === 'password') {
77
            return $this->passwordInput($prop, $label);
78
        } elseif ($prop->type() === 'boolean') {
79
            return $this->booleanInput($prop, $label);
80
        } else {
81
            $input = $climate->input($label);
82
            if ($prop->type() === 'text' || $prop->type === 'html') {
0 ignored issues
show
Accessing type on the interface Charcoal\Property\PropertyInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
83
                $input->multiLine();
84
            }
85
        }
86
        return $input;
87
    }
88
89
    /**
90
     * Get a CLI input from a boolean property.
91
     *
92
     * @param  PropertyInterface $prop  The property to retrieve input from.
93
     * @param  string            $label The input label.
94
     * @return LeagueInput The League's terminal input object.
95
     */
96
    private function booleanInput(PropertyInterface $prop, $label)
97
    {
98
        $climate = $this->climate();
99
100
        $opts = [
101
            1 => $prop->trueLabel(),
0 ignored issues
show
The method trueLabel() does not exist on Charcoal\Property\PropertyInterface. It seems like you code against a sub-type of Charcoal\Property\PropertyInterface such as Charcoal\Property\BooleanProperty. ( Ignorable by Annotation )

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

101
            1 => $prop->/** @scrutinizer ignore-call */ trueLabel(),
Loading history...
102
            0 => $prop->falseLabel()
0 ignored issues
show
The method falseLabel() does not exist on Charcoal\Property\PropertyInterface. It seems like you code against a sub-type of Charcoal\Property\PropertyInterface such as Charcoal\Property\BooleanProperty. ( Ignorable by Annotation )

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

102
            0 => $prop->/** @scrutinizer ignore-call */ falseLabel()
Loading history...
103
        ];
104
        $input = $climate->radio(
105
            $label,
106
            $opts
107
        );
108
        return $input;
109
    }
110
111
    /**
112
     * Get a CLI password input (hidden) from a password property.
113
     *
114
     * @param  PropertyInterface $prop  The property to retrieve input from.
115
     * @param  string            $label The input label.
116
     * @return LeagueInput The League's terminal input object.
117
     */
118
    private function passwordInput(PropertyInterface $prop, $label)
119
    {
120
        unset($prop);
121
122
        $climate = $this->climate();
123
124
        $input = $climate->password($label);
125
        return $input;
126
    }
127
128
    /**
129
     * Set the model factory.
130
     *
131
     * @param  FactoryInterface $factory The factory used to create models.
132
     * @return void
133
     */
134
    private function setModelFactory(FactoryInterface $factory)
135
    {
136
        $this->modelFactory = $factory;
137
    }
138
139
    /**
140
     * Retrieve the model factory.
141
     *
142
     * @return FactoryInterface
143
     */
144
    protected function modelFactory()
145
    {
146
        return $this->modelFactory;
147
    }
148
}
149