RedisBusinessFactory::createRedisClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Xervice\Redis\Business;
6
7
8
use Predis\Client;
9
use Xervice\Core\Business\Model\Factory\AbstractBusinessFactory;
10
use Xervice\DataProvider\Business\Model\DataProvider\AbstractDataProvider;
11
use Xervice\Redis\Business\Model\Commands\Collection;
12
use Xervice\Redis\Business\Model\Commands\Provider;
13
use Xervice\Redis\Business\Model\Commands\ProviderInterface;
14
use Xervice\Redis\Business\Model\Converter\DataConverter;
15
use Xervice\Redis\Business\Model\Converter\DataConverterInterface;
16
use Xervice\Redis\Business\Model\Converter\ListConverter;
17
use Xervice\Redis\Business\Model\Converter\ListConverterInterface;
18
use Xervice\Redis\Business\Model\Transaction\Transaction;
19
use Xervice\Redis\Business\Model\Transaction\TransactionCollection;
20
use Xervice\Redis\Business\Model\Transaction\TransactionHandler;
21
use Xervice\Redis\Business\Model\Transaction\TransactionHandlerInterface;
22
use Xervice\Redis\Business\Model\Transaction\TransactionInterface;
23
use Xervice\Redis\RedisDependencyProvider;
24
25
/**
26
 * @method \Xervice\Redis\RedisConfig getConfig()
27
 */
28
class RedisBusinessFactory extends AbstractBusinessFactory
29
{
30
    /**
31
     * @var \Predis\Client
32
     */
33
    private $client;
34
35
    /**
36
     * @var \Xervice\Redis\Business\Model\Transaction\TransactionHandlerInterface
37
     */
38
    private $transationHandler;
39
40
    /**
41
     * @param string $key
42
     * @param \Xervice\DataProvider\Business\Model\DataProvider\AbstractDataProvider $dataProvider
43
     *
44
     * @return \Xervice\Redis\Business\Model\Transaction\TransactionInterface
45
     */
46 1
    public function createTransaction(string $key, AbstractDataProvider $dataProvider) : TransactionInterface
47
    {
48 1
        return new Transaction(
49 1
            $key,
50 1
            $dataProvider
51
        );
52
    }
53
54
    /**
55
     * @return \Xervice\Redis\Business\Model\Transaction\TransactionHandlerInterface
56
     */
57 1
    public function createTransactionHandler() : TransactionHandlerInterface
58
    {
59 1
        return new TransactionHandler(
60 1
            $this->createTransactionCollection()
61
        );
62
    }
63
64
    /**
65
     * @return \Xervice\Redis\Business\Model\Transaction\TransactionCollection
66
     */
67 1
    public function createTransactionCollection() : TransactionCollection
68
    {
69 1
        return new TransactionCollection();
70
    }
71
72
    /**
73
     * @return \Xervice\Redis\Business\Model\Converter\ListConverterInterface
74
     */
75 3
    public function createListConverter() : ListConverterInterface
76
    {
77 3
        return new ListConverter(
78 3
            $this->createConverter()
79
        );
80
    }
81
82
    /**
83
     * @return \Xervice\Redis\Business\Model\Converter\DataConverterInterface
84
     */
85 6
    public function createConverter(): DataConverterInterface
86
    {
87 6
        return new DataConverter();
88
    }
89
90
    /**
91
     * @return \Xervice\Redis\Business\Model\Commands\ProviderInterface
92
     */
93 8
    public function createCommandProvider(): ProviderInterface
94
    {
95 8
        return new Provider(
96 8
            $this->getCommandCollection(),
97 8
            $this->getRedisClient()
98
        );
99
    }
100
101
    /**
102
     * @return \Predis\Client
103
     */
104 1
    public function createRedisClient(): Client
105
    {
106 1
        return new Client(
107 1
            $this->getConfig()->getRedisConfig(),
108 1
            $this->getConfig()->getRedisOptions()
109
        );
110
    }
111
112
    /**
113
     * @return \Predis\Client
114
     */
115 8
    public function getRedisClient(): Client
116
    {
117 8
        if ($this->client === null) {
118 1
            $this->client = $this->createRedisClient();
119
        }
120 8
        return $this->client;
121
    }
122
123
    /**
124
     * @return \Xervice\Redis\Business\Model\Commands\Collection
125
     */
126 8
    public function getCommandCollection(): Collection
127
    {
128 8
        return $this->getDependency(RedisDependencyProvider::REDIS_COMMAND_COLLECTION);
129
    }
130
131
    /**
132
     * @return \Xervice\Redis\Business\Model\Transaction\TransactionHandlerInterface
133
     */
134 1
    public function getTransactionHandler() : TransactionHandlerInterface
135
    {
136 1
        if ($this->transationHandler === null) {
137 1
            $this->transationHandler = $this->createTransactionHandler();
138
        }
139
140 1
        return $this->transationHandler;
141
    }
142
}
143