TelegramApiFactory::setConfigFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram\Factory;
6
7
use Zored\Telegram\Madeline\Api\ApiConstructor;
8
use Zored\Telegram\Madeline\Api\ApiConstructorInterface;
9
use Zored\Telegram\Madeline\ApiFactory;
10
use Zored\Telegram\Madeline\ApiFactoryInterface;
11
use Zored\Telegram\Madeline\Config\Builder\ConfigFactoryInterface;
12
use Zored\Telegram\Madeline\Config\Builder\EnvConfigFactory;
13
use Zored\Telegram\Madeline\Config\ConfigInterface;
14
use Zored\Telegram\Madeline\Config\Extractor\ConfigExtractor;
15
use Zored\Telegram\Madeline\Config\Extractor\ConfigExtractorInterface;
16
use Zored\Telegram\Serializer\Jms\JmsSerializer;
17
use Zored\Telegram\Serializer\SerializerInterface;
18
use Zored\Telegram\TelegramApi;
19
use Zored\Telegram\TelegramApiInterface;
20
21
class TelegramApiFactory implements TelegramApiFactoryInterface
22
{
23
    /**
24
     * @var ConfigExtractorInterface|null
25
     */
26
    private $configExtractor;
27
28
    /**
29
     * @var ApiConstructorInterface|null
30
     */
31
    private $apiConstructor;
32
33
    /**
34
     * @var ApiFactoryInterface|null
35
     */
36
    private $apiFactory;
37
38
    /**
39
     * @var SerializerInterface|null
40
     */
41
    private $serializer;
42
43
    /**
44
     * @var ConfigFactoryInterface|null
45
     */
46
    private $configFactory;
47
48
    /**
49
     * @var TelegramApi|null
50
     */
51
    private $api;
52
53
    public function create(): TelegramApiInterface
54
    {
55
        if ($this->api) {
56
            return $this->api;
57
        }
58
59
        $config = $this->setDefaults();
60
61
        return $this->api = new TelegramApi(
62
            $this->apiFactory->create($config, $this->apiConstructor),
63
            $this->serializer
64
        );
65
    }
66
67
    public function setConfigExtractor(ConfigExtractorInterface $configExtractor): self
68
    {
69
        $this->configExtractor = $configExtractor;
70
71
        return $this;
72
    }
73
74
    public function setApiConstructor(ApiConstructorInterface $apiConstructor): self
75
    {
76
        $this->apiConstructor = $apiConstructor;
77
78
        return $this;
79
    }
80
81
    public function setApiFactory(ApiFactoryInterface $apiFactory): self
82
    {
83
        $this->apiFactory = $apiFactory;
84
85
        return $this;
86
    }
87
88
    public function setSerializer(?SerializerInterface $serializer): self
89
    {
90
        $this->serializer = $serializer;
91
92
        return $this;
93
    }
94
95
    public function setConfigFactory(ConfigFactoryInterface $configFactory): self
96
    {
97
        $this->configFactory = $configFactory;
98
99
        return $this;
100
    }
101
102
    private function setDefaults(): ConfigInterface
103
    {
104
        $this->configExtractor = $this->configExtractor ?? new ConfigExtractor();
105
        $this->apiFactory = $this->apiFactory ?? new ApiFactory();
106
        $this->serializer = $this->serializer ?? new JmsSerializer();
107
        $this->configFactory = $this->configFactory ?? new EnvConfigFactory();
108
109
        $config = $this->configFactory->create();
110
        $this->apiConstructor = $this->apiConstructor ?? new ApiConstructor($config, $this->configExtractor);
111
112
        return $config;
113
    }
114
}
115