GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Ingest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Psonic;
4
5
use Psonic\Channels\Channel;
6
use Psonic\Contracts\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Psonic\Client. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use InvalidArgumentException;
8
use Psonic\Commands\Ingest\PopCommand;
9
use Psonic\Commands\Ingest\PushCommand;
10
use Psonic\Commands\Ingest\CountCommand;
11
use Psonic\Commands\Ingest\FlushObjectCommand;
12
use Psonic\Commands\Ingest\FlushBucketCommand;
13
use Psonic\Commands\Ingest\FlushCollectionCommand;
14
use Psonic\Commands\Ingest\StartIngestChannelCommand;
15
16
class Ingest extends Channel
17
{
18
    /**
19
     * Ingest constructor.
20
     * @param Client $client
21
     */
22
    public function __construct(Client $client)
23
    {
24
        parent::__construct($client);
25
    }
26
27
    /**
28
     * @return mixed|Contracts\Response|void
29
     * @throws Exceptions\ConnectionException
30
     */
31
    public function connect($password = 'SecretPassword')
32
    {
33
        parent::connect();
34
35
        $response = $this->send(new StartIngestChannelCommand($password));
36
37
        if ($bufferSize = $response->get('bufferSize')) {
38
            $this->bufferSize = (int)$bufferSize;
39
        }
40
41
        return $response;
42
    }
43
44
    /**
45
     * @param string $collection
46
     * @param string $bucket
47
     * @param string $object
48
     * @param string $text
49
     * @param string $locale
50
     * @return Contracts\Response
51
     */
52
    public function push(string $collection, string $bucket, string $object, string $text, $locale = null)
53
    {
54
55
        $chunks = $this->splitString($collection, $bucket, $object, $text);
56
57
        if ($text == "" || empty($chunks)) {
58
            throw new InvalidArgumentException("The parameter \$text is empty");
59
        }
60
        foreach ($chunks as $chunk) {
61
            $message = $this->send(new PushCommand($collection, $bucket, $object, $chunk, $locale));
62
            if ($message == false || $message == "") {
63
                throw new InvalidArgumentException();
64
            }
65
        }
66
        return $message;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message seems to be defined by a foreach iteration on line 60. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
67
    }
68
69
    /**
70
     * @param string $collection
71
     * @param string $bucket
72
     * @param string $object
73
     * @param string $text
74
     * @return mixed
75
     */
76
    public function pop(string $collection, string $bucket, string $object, string $text)
77
    {
78
        $chunks = $this->splitString($collection, $bucket, $object, $text);
79
        $count  = 0;
80
        foreach ($chunks as $chunk) {
81
            $message = $this->send(new PopCommand($collection, $bucket, $object, $chunk));
82
            if ($message == false || $message == "") {
83
                throw new InvalidArgumentException();
84
            }
85
            $count += $message->get('count');
86
        }
87
88
        return $count;
89
    }
90
91
    /**
92
     * @param $collection
93
     * @param null $bucket
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $bucket is correct as it would always require null to be passed?
Loading history...
94
     * @param null $object
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $object is correct as it would always require null to be passed?
Loading history...
95
     * @return mixed
96
     */
97
    public function count($collection, $bucket = null, $object = null)
98
    {
99
        $message = $this->send(new CountCommand($collection, $bucket, $object));
100
101
        return $message->get('count');
102
    }
103
104
    /**
105
     * @param $collection
106
     * @return mixed
107
     */
108
    public function flushc($collection)
109
    {
110
        $message = $this->send(new FlushCollectionCommand($collection));
111
        return $message->getCount();
0 ignored issues
show
Bug introduced by
The method getCount() does not exist on Psonic\Contracts\Response. Since it exists in all sub-types, consider adding an abstract or default implementation to Psonic\Contracts\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        return $message->/** @scrutinizer ignore-call */ getCount();
Loading history...
112
    }
113
114
    /**
115
     * @param $collection
116
     * @param $bucket
117
     * @return integer
118
     */
119
    public function flushb($collection, $bucket)
120
    {
121
        $message = $this->send(new FlushBucketCommand($collection, $bucket));
122
        return $message->getCount();
123
    }
124
125
    /**
126
     * @param $collection
127
     * @param $bucket
128
     * @param $object
129
     * @return mixed
130
     */
131
    public function flusho($collection, $bucket, $object)
132
    {
133
        $message = $this->send(new FlushObjectCommand($collection, $bucket, $object));
134
        return $message->getCount();
135
    }
136
137
    /**
138
     * @param string $collection
139
     * @param string $bucket
140
     * @param string $key
141
     * @param string $text
142
     * @return array
143
     */
144
    private function splitString(string $collection, string $bucket, string $key, string  $text): array
145
    {
146
        return str_split($text, ($this->bufferSize - (strlen($key . $collection . $bucket) + 20)));
147
    }
148
}
149