Completed
Branch master (995814)
by Juuso
11:40 queued 06:36
created

AlgoliaComponent::generateConfig()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 9
nc 2
nop 0
crap 12
1
<?php
2
3
namespace leinonen\Yii2Algolia;
4
5
use AlgoliaSearch\Client;
6
use InvalidArgumentException;
7
use leinonen\Yii2Algolia\ActiveRecord\ActiveRecordFactory;
8
use Yii;
9
use yii\base\Application;
10
use yii\base\BootstrapInterface;
11
use yii\base\Component;
12
use yii\base\InvalidConfigException;
13
14
/**
15
 * @method Client getClient()
16
 * @method array getConfig()
17
 * @method pushToIndices(SearchableInterface $searchableModel)
18
 * @method updateInIndices(SearchableInterface $searchableModel)
19
 * @method removeFromIndices(SearchableInterface $searchableModel)
20
 * @method reindex(string $className)
21
 * @method clearIndices(string $className)
22
 * @method setConnectTimeout(int $connectTimeout, int $timeout = 30, int $searchTimeout = 5)
23
 * @method enableRateLimitForward(string $adminAPIKey, string $endUserIP, string $rateLimitAPIKey)
24
 * @method setForwarderFor(string $ip)
25
 * @method setAlgoliaUserToken(string $token)
26
 * @method disableRateLimitForward()
27
 * @method isAlive()
28
 * @method setExtraHeader(string $key, string $value)
29
 * @method mixed multipleQueries(array $queries, string $indexNameKey = "indexName", string $strategy = "none")
30
 * @method mixed listIndexes()
31
 * @method deleteIndex(string $indexName)
32
 * @method mixed moveIndex(string $srcIndexName, string $dstIndexName)
33
 * @method mixed copyIndex(string $srcIndexName, string $dstIndexName)
34
 * @method mixed getLogs(int $offset = 0, int $length = 10, string $type = "all")
35
 * @method \AlgoliaSearch\Index initIndex(string $indexName)
36
 * @method mixed listUserKeys()
37
 * @method mixed getUserKeyACL(string $key)
38
 * @method mixed deleteUserKey(string $key)
39
 * @method mixed addUserKey(array $obj, int $validity = 0, int $maxQueriesPerIPPerHour = 0, int $maxHitsPerQuery = 0, array $indexes = null)
40
 * @method mixed updateUserKey(string $key, array $obj, int $validity = 0, int $maxQueriesPerIPPerHour = 0, int $maxHitsPerQuery = 0, array $indexes = null)
41
 * @method mixed batch(array $requests)
42
 * @method string generateSecuredApiKey(string $privateApiKey, mixed $query, string $userToken = null)
43
 * @method string buildQuery(array $args)
44
 * @method mixed request(Client $context, string $method, string $path, array $params, array $data, array $hostsArray, int $connectTimeout, int $readTimeout)
45
 * @method mixed doRequest(Client $context, string $method, string $path, array $params, array $data, array $hostsArray, int $connectTimeout, int $readTimeout)
46
 * @method \AlgoliaSearch\PlacesIndex initPlaces(string $appId, string $appKey, array $hostsArray = null, array $options = [])
47
 * @see Client
48
 * @see AlgoliaManager
49
 */
50
class AlgoliaComponent extends Component implements BootstrapInterface
51
{
52
    /**
53
     * @var string The application ID you have in your admin interface
54
     */
55
    public $applicationId;
56
57
    /**
58
     * @var string A valid API key for the service
59
     */
60
    public $apiKey;
61
62
    /**
63
     * @var null|array The list of hosts that you have received for the service
64
     */
65
    public $hostsArray = null;
66
67
    /**
68
     * @var array
69
     */
70
    public $options = [];
71
72
    /**
73
     * @var null|string
74
     */
75
    public $env = null;
76
77
    /**
78
     * @var AlgoliaManager
79
     */
80
    protected $manager;
81
82
    /**
83
     * @var AlgoliaFactory
84
     */
85
    private $algoliaFactory;
86
87 22
    /**
88 5
     * @var ActiveRecordFactory
89 22
     */
90 22
    private $activeRecordFactory;
91
92
    /**
93
     * Initiates a new AlgoliaComponent.
94
     *
95
     * @param AlgoliaFactory $algoliaFactory
96
     * @param ActiveRecordFactory $activeRecordFactory
97 22
     * @param array $config
98
     */
99 22
    public function __construct(AlgoliaFactory $algoliaFactory, ActiveRecordFactory $activeRecordFactory, $config = [])
100 22
    {
101
        $this->algoliaFactory = $algoliaFactory;
102
        $this->activeRecordFactory = $activeRecordFactory;
103
104
        parent::__construct($config);
105
    }
106
107 22
    /**
108
     * Bootstrap method to be called during application bootstrap stage.
109 22
     *
110 22
     * @param Application $app the application currently running
111
     */
112 22
    public function bootstrap($app)
113 22
    {
114 22
        Yii::$container->set(AlgoliaManager::class, function () {
115 22
            return $this->createManager();
116 22
        });
117
    }
118 22
119 22
    /**
120
     * {@inheritdoc}
121 22
     */
122
    public function init()
123
    {
124
        $this->manager = $this->createManager();
125
    }
126
127
    /**
128
     * Returns a new AlgoliaManager.
129
     *
130
     * @return AlgoliaManager
131
     */
132 1
    protected function createManager()
133
    {
134 1
        $config = $this->generateConfig();
135
        $client = $this->algoliaFactory->make($config);
136
137
        $algoliaManager = new AlgoliaManager($client, $this->activeRecordFactory);
138
        $algoliaManager->setEnv($this->env);
139
140
        return $algoliaManager;
141
    }
142
143
    /**
144
     * Dynamically pass methods to the AlgoliaManager.
145
     *
146
     * @param string $method
147
     * @param array $parameters
148
     *
149
     * @return mixed
150
     */
151
    public function __call($method, $parameters)
152
    {
153
        return call_user_func_array([$this->manager, $method], $parameters);
154
    }
155
156
    /**
157
     * Generates config for the Algolia Manager.
158
     *
159
     * @return AlgoliaConfig
160
     * @throws \Exception
161
     */
162
    private function generateConfig()
163
    {
164
        if (empty($this->applicationId) || empty($this->apiKey)) {
165
            throw new \Exception('applicationId and apiKey are required');
166
        }
167
168
        $config = new AlgoliaConfig(
169
            $this->applicationId,
170
            $this->apiKey,
171
            $this->hostsArray,
172
            $this->options
173
        );
174
175
        return $config;
176
    }
177
}
178