Completed
Push — master ( 061f73...105351 )
by Yuichi
10s
created

Config::setUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace CybozuHttpBundle\Cybozu;
4
5
use CybozuHttpBundle\Entity\CybozuAccountInterface;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7
8
/**
9
 * @author ochi51 <[email protected]>
10
 */
11
class Config
12
{
13
14
    /**
15
     * @var TokenStorageInterface
16
     */
17
    private $ts;
18
19
    /**
20
     * @var CybozuAccountInterface
21
     */
22
    private $user;
23
24
    /**
25
     * @var string
26
     */
27
    private $certDir;
28
29
    /**
30
     * @var string
31
     */
32
    private $logfile;
33
34
    /**
35
     * @param TokenStorageInterface $ts
36
     * @param string|null $certDir
37
     * @param string|null $logfile
38
     */
39 3
    public function __construct(TokenStorageInterface $ts, $certDir = null, $logfile = null)
40
    {
41 3
        $this->ts = $ts;
42 3
        $this->certDir = $certDir;
43 3
        $this->logfile = $logfile;
44
45 3
        $token = $ts->getToken();
46 3
        if ($token && $token->getUser() instanceof CybozuAccountInterface) {
47 2
            $this->user = $token->getUser();
48 2
        }
49 3
    }
50
51
    /**
52
     * @return array
53
     */
54 3
    public function getConfig()
55
    {
56 3
        if ($this->user instanceof CybozuAccountInterface) {
57 3
            $config = $this->user->getCybozuHttpConfig();
58 3
            $config['debug']   = $this->user->getDebugMode();
59 3
            $config['logfile'] = $this->logfile;
60
            // This assumes to use KnpGaufretteBundle and VichUploaderBundle.
61
            // Entity has only key, so can't know directory.
62 3
            if (array_key_exists('cert_file', $config)) {
63 3
                $config['cert_file'] = $this->certDir . '/' . $config['cert_file'];
64 3
            }
65
66 3
            return $config;
67
        }
68
69 1
        throw new \RuntimeException('User in token and this user done not implement CybozuAccountInterface.');
70
    }
71
72
    /**
73
     * @param CybozuAccountInterface $user
74
     * @return self
75
     */
76 1
    public function setUser(CybozuAccountInterface $user)
77
    {
78 1
        $this->user = $user;
79
80 1
        return $this;
81
    }
82
}