Completed
Pull Request — master (#22)
by Sergey
15:20 queued 20s
created

ElasticsearchModel::belongsTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Isswp101\Persimmon\Model;
4
5
use Elasticsearch\ClientBuilder;
6
use Isswp101\Persimmon\Collection\IElasticsearchCollection;
7
use Isswp101\Persimmon\DI\Container;
8
use Isswp101\Persimmon\DI\DI;
9
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder;
10
use Isswp101\Persimmon\Relationship\BelongsToRelationship;
11
use Isswp101\Persimmon\Relationship\HasManyRelationship;
12
use Isswp101\Persimmon\Repository\ElasticsearchRepository;
13
use Isswp101\Persimmon\Repository\RuntimeCacheRepository;
14
15
/**
16
 * @method static static|null find(string $id, array $columns = [])
17
 * @method static IElasticsearchCollection all(IQueryBuilder $query, callable $callback = null)
18
 */
19
class ElasticsearchModel extends Eloquent implements IElasticsearchModel
20
{
21
    protected static function di(): Container
22
    {
23
        $container = DI::make('elasticsearch');
24
        if ($container == null) {
25
            DI::bind('elasticsearch', function () {
26
                $client = ClientBuilder::create()->build();
27
                return new Container(new ElasticsearchRepository($client), new RuntimeCacheRepository());
28
            }, DI::SINGLETON);
29
            $container = DI::make('elasticsearch');
30
        }
31
        return $container;
32
    }
33
34
    protected function belongsTo(string $class): BelongsToRelationship
35
    {
36
        return new BelongsToRelationship($this, $class);
37
    }
38
39
    protected function hasMany(string $class): HasManyRelationship
40
    {
41
        return new HasManyRelationship($this, $class);
42
    }
43
}