Passed
Push — master ( caf566...d31b80 )
by Mina
03:05
created

IndexInitializationTrait::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mina
5
 * Date: 3/26/17
6
 * Time: 3:38 PM
7
 */
8
9
namespace MAbadir\ElasticLaravel\Console;
10
11
use MAbadir\ElasticLaravel\ElasticClient;
12
13
trait IndexInitializationTrait
14
{
15
    /**
16
     * Basic standard configuration
17
     * @var array
18
     */
19
    protected $params = [
20
        "body" => [
21
            "settings" => [
22
                "number_of_shards" => 1,
23
                "number_of_replicas" => 0,
24
            ],
25
        ],
26
    ];
27
28
    /**
29
     * Holds the ElasticClient
30
     * @var \MAbadir\ElasticLaravel\ElasticClient
31
     */
32
    protected $client;
33
34
    /**
35
     * IndexInitializationTrait constructor.
36
     *
37
     * @param \MAbadir\ElasticLaravel\ElasticClient $client
38
     */
39
    public function __construct(ElasticClient $client)
40
    {
41
        parent::__construct();
42
        $this->client = $client;
43
    }
44
45
    /**
46
     * Execute the console command.
47
     *
48
     * @return mixed
49
     */
50
    public function handle()
51
    {
52
        if ($this->confirm('Are you sure you want to proceed, this will destroy the Search Index? [y|N]'))
0 ignored issues
show
Bug introduced by
The method confirm() does not exist on MAbadir\ElasticLaravel\C...ndexInitializationTrait. Did you maybe mean confirmPassword()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
53
        {
54
            return $this->confirmPassword();
55
        }
56
    }
57
58
    /**
59
     * Confirms user awareness before proceeding
60
     *
61
     * @return    mixed
62
     * @author    Mina Abadir <[email protected]>
63
     * @copyright Copyright (c) 2016, Mina Abadir
64
     */
65
    protected function confirmPassword()
66
    {
67
        $password = str_random(7);
68
        $input = $this->ask('For safety please enter this text "'.$password.'"');
0 ignored issues
show
Bug introduced by
It seems like ask() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
        if($input == $password){
70
            return $this->buildIndex();
71
        }
72
        else {
73
            return $this->error('Password is incorrect!');
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
        }
75
    }
76
77
    /**
78
     * Deletes, Creates the Index then puts the settings
79
     *
80
     * @return    mixed
81
     * @author    Mina Abadir <[email protected]>
82
     * @copyright Copyright (c) 2016, Mina Abadir
83
     */
84
    protected function buildIndex()
85
    {
86
        $this->client->dropIndex();
87
        $response = $this->client->createIndex($this->params);
88
89
        if(is_array($response) && $response['acknowledged'] == true)
90
        {
91
            $this->info('Index Initialization Successful');
0 ignored issues
show
Bug introduced by
It seems like info() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
92
        }
93
        else{
94
            $this->error('Index Initialization Failed');
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
95
        }
96
    }
97
}