summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/influxdb/src/InfluxDbConnectionFactory.php
blob: f260010d7aa11ba76e592108fc21774d71b238e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

namespace gipfl\InfluxDb;

use gipfl\Curl\CurlAsync;
use React\EventLoop\LoopInterface;
use React\Promise\Promise;
use RuntimeException;

abstract class InfluxDbConnectionFactory
{
    /**
     * AsyncInfluxDbWriter constructor.
     * @param LoopInterface $loop
     * @param $baseUrl string InfluxDB base URL
     * @param string|null $username
     * @param string|null $password
     * @return Promise <InfluxDbConnection>
     */
    public static function create(CurlAsync $curl, $baseUrl, $username = null, $password = null)
    {
        $v1 = new InfluxDbConnectionV1($curl, $baseUrl);
        return $v1->getVersion()->then(function ($version) use ($baseUrl, $username, $password, $curl, $v1) {
            if ($version === null || preg_match('/^v?2\./', $version)) {
                $v2 = new InfluxDbConnectionV2($curl, $baseUrl, $username, $password);
                return $v2->getVersion()->then(function ($version) use ($v2) {
                    if ($version === null) {
                        throw new RuntimeException('Unable to detect InfluxDb version');
                    } else {
                        return $v2;
                    }
                });
            } else {
                return $v1;
            }
        });
    }
}