StartableEndpoint::start()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 7
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 12
1
<?php
2
namespace PSB\Core;
3
4
5
use PSB\Core\Feature\Feature;
6
use PSB\Core\Feature\FeatureActivator;
7
use PSB\Core\Feature\FeatureInstaller;
8
use PSB\Core\Feature\FeatureStarter;
9
use PSB\Core\ObjectBuilder\BuilderInterface;
10
use PSB\Core\Persistence\PersistenceDefinitionApplier;
11
use PSB\Core\Pipeline\PipelineModifications;
12
use PSB\Core\Transport\Config\TransportDefinition;
13
use PSB\Core\Transport\Config\TransportInfrastructure;
14
use PSB\Core\Transport\TransportReceiver;
15
use PSB\Core\Util\Settings;
16
17
class StartableEndpoint
18
{
19
    /**
20
     * @var Settings
21
     */
22
    private $settings;
23
24
    /**
25
     * @var PersistenceDefinitionApplier
26
     */
27
    private $persistenceDefinitionApplier;
28
29
    /**
30
     * @var BuilderInterface
31
     */
32
    private $builder;
33
34
    /**
35
     * @var PipelineModifications
36
     */
37
    private $pipelineModifications;
38
39
    /**
40
     * @var BusContextInterface
41
     */
42
    private $busContext;
43
44
    /**
45
     * @var bool
46
     */
47
    private $isPrepared = false;
48
49
50
    /**
51
     * @param Settings                     $settings
52
     * @param PersistenceDefinitionApplier $persistenceDefinitionApplier
53
     * @param BuilderInterface             $builder
54
     * @param PipelineModifications        $pipelineModifications
55
     * @param BusContextInterface          $busContext
56
     */
57
    public function __construct(
58
        Settings $settings,
59
        PersistenceDefinitionApplier $persistenceDefinitionApplier,
60
        BuilderInterface $builder,
61
        PipelineModifications $pipelineModifications,
62
        BusContextInterface $busContext
63
    ) {
64
        $this->settings = $settings;
65
        $this->persistenceDefinitionApplier = $persistenceDefinitionApplier;
66
        $this->builder = $builder;
67
        $this->pipelineModifications = $pipelineModifications;
68
        $this->busContext = $busContext;
69
    }
70
71
72
    /**
73
     * @return StartableEndpoint
74
     */
75
    public function prepare()
76
    {
77
        $this->persistenceDefinitionApplier->apply($this->settings);
78
79
        $featureActivator = new FeatureActivator($this->settings);
80
81
        foreach ($this->settings->get(KnownSettingsEnum::FEATURE_FQCN_LIST) as $featureFqcn) {
82
            /** @var Feature $feature */
83
            $feature = new $featureFqcn();
84
            $feature->describe();
85
            $featureActivator->addFeature($feature);
86
        }
87
88
        $featureInstaller = new FeatureInstaller($featureActivator->getFeatures());
89
        $featureStarter = new FeatureStarter($featureActivator->getFeatures());
90
91
92
        /** @var TransportDefinition $transportDefinition */
93
        $transportDefinition = $this->settings->get(TransportDefinition::class);
94
        $this->settings->set(
95
            TransportInfrastructure::class,
96
            $transportDefinition->formalize(
97
                $this->settings,
98
                $transportDefinition->createConnectionFactory($this->settings)
99
            )
100
        );
101
102
        $featureActivator->activateFeatures($this->builder, $this->pipelineModifications);
103
104
        $featureInstaller->installFeatures($this->builder, $this->settings);
105
106
        $this->pipelineModifications->registerStepsInBuilder($this->builder);
107
108
        $featureStarter->startFeatures($this->builder, $this->busContext);
109
110
        $this->isPrepared = true;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return EndpointInstance
117
     */
118
    public function start()
119
    {
120
        if (!$this->isPrepared) {
121
            $this->prepare();
122
        }
123
124
        if (!$this->settings->tryGet(KnownSettingsEnum::SEND_ONLY)) {
125
            /** @var TransportReceiver $transportReceiver */
126
            $transportReceiver = $this->builder->build(TransportReceiver::class);
127
            $transportReceiver->start();
128
        }
129
130
        return new EndpointInstance($this->busContext);
131
    }
132
133
    /**
134
     * @return BusContextInterface
135
     */
136
    public function getBusContext()
137
    {
138
        return $this->busContext;
139
    }
140
141
    /**
142
     * @return boolean
143
     */
144
    public function isIsPrepared()
145
    {
146
        return $this->isPrepared;
147
    }
148
149
    /**
150
     * @return Settings
151
     */
152
    public function getSettings()
153
    {
154
        return $this->settings;
155
    }
156
157
    /**
158
     * @return BuilderInterface
159
     */
160
    public function getBuilder()
161
    {
162
        return $this->builder;
163
    }
164
165
    /**
166
     * @return PipelineModifications
167
     */
168
    public function getPipelineModifications()
169
    {
170
        return $this->pipelineModifications;
171
    }
172
}
173