Completed
Pull Request — master (#22)
by Sergey
14:20
created

ElasticsearchModel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 0
cbo 8
dl 0
loc 25
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A di() 0 12 2
A belongsTo() 0 4 1
A hasMany() 0 4 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
}