CommandBuilder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 10 2
A prepareCommand() 0 8 3
A __construct() 0 5 1
1
<?php
2
3
namespace diecoding\aws\s3;
4
5
use diecoding\aws\s3\interfaces;
6
7
/**
8
 * Class CommandBuilder
9
 *
10
 * @package diecoding\aws\s3
11
 */
12
class CommandBuilder implements interfaces\CommandBuilder
13
{
14
    /** @var string default bucket name */
15
    protected $bucket;
16
17
    /** @var string default acl */
18
    protected $acl;
19
20
    /** @var interfaces\Bus */
21
    protected $bus;
22
23
    /**
24
     * CommandBuilder constructor.
25
     *
26
     * @param \diecoding\aws\s3\interfaces\Bus $bus
27
     * @param string                                 $bucket
28
     * @param string                                 $acl
29
     */
30
    public function __construct(interfaces\Bus $bus, string $bucket = '', string $acl = '')
31
    {
32
        $this->bus = $bus;
33
        $this->bucket = $bucket;
34
        $this->acl = $acl;
35
    }
36
37
    /**
38
     * @param string $className
39
     *
40
     * @return \diecoding\aws\s3\interfaces\commands\Command
41
     * @throws \yii\base\InvalidConfigException
42
     */
43
    public function build(string $className): interfaces\commands\Command
44
    {
45
        $params = is_subclass_of($className, interfaces\commands\ExecutableCommand::class) ? [$this->bus] : [];
46
47
        /** @var interfaces\commands\Command $command */
48
        $command = \Yii::createObject($className, $params);
49
50
        $this->prepareCommand($command);
51
52
        return $command;
53
    }
54
55
    /**
56
     * @param \diecoding\aws\s3\interfaces\commands\Command $command
57
     */
58
    protected function prepareCommand(interfaces\commands\Command $command)
59
    {
60
        if ($command instanceof interfaces\commands\HasBucket) {
61
            $command->inBucket($this->bucket);
62
        }
63
64
        if ($command instanceof interfaces\commands\HasAcl) {
65
            $command->withAcl($this->acl);
66
        }
67
    }
68
}
69