Completed
Push — master ( 995814...b8a5e6 )
by Juuso
12:56 queued 08:00
created

AlgoliaComponent::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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
    /**
88
     * @var ActiveRecordFactory
89
     */
90
    private $activeRecordFactory;
91
92
    /**
93
     * Initiates a new AlgoliaComponent.
94
     *
95
     * @param AlgoliaFactory $algoliaFactory
96
     * @param ActiveRecordFactory $activeRecordFactory
97
     * @param array $config
98
     */
99 24
    public function __construct(AlgoliaFactory $algoliaFactory, ActiveRecordFactory $activeRecordFactory, $config = [])
100
    {
101 24
        $this->algoliaFactory = $algoliaFactory;
102 24
        $this->activeRecordFactory = $activeRecordFactory;
103
104 24
        parent::__construct($config);
105 24
    }
106
107
    /**
108
     * Bootstrap method to be called during application bootstrap stage.
109
     *
110
     * @param Application $app the application currently running
111
     */
112
    public function bootstrap($app)
113
    {
114 24
        Yii::$container->set(AlgoliaManager::class, function () {
115 5
            return $this->createManager();
116 24
        });
117 24
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 24
    public function init()
123
    {
124 24
        $this->manager = $this->createManager();
125 24
    }
126
127
    /**
128
     * Returns a new AlgoliaManager.
129
     *
130
     * @return AlgoliaManager
131
     */
132 24
    protected function createManager()
133
    {
134 24
        $config = $this->generateConfig();
135 24
        $client = $this->algoliaFactory->make($config);
136
137 24
        $algoliaManager = new AlgoliaManager($client, $this->activeRecordFactory);
138 24
        $algoliaManager->setEnv($this->env);
139
140 24
        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 1
    public function __call($method, $parameters)
152
    {
153 1
        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 24
    private function generateConfig()
163
    {
164 24
        if (empty($this->applicationId) || empty($this->apiKey)) {
165 2
            throw new \Exception('applicationId and apiKey are required');
166
        }
167
168 24
        $config = new AlgoliaConfig(
169 24
            $this->applicationId,
170 24
            $this->apiKey,
171 24
            $this->hostsArray,
172 24
            $this->options
173 24
        );
174
175 24
        return $config;
176
    }
177
}
178