Passed
Branch master (2d34dd)
by Juuso
05:59
created

AlgoliaComponent::createManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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