ppshobi /
psonic
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Psonic; |
||||
| 4 | |||||
| 5 | use Psonic\Channels\Channel; |
||||
| 6 | use Psonic\Contracts\Client; |
||||
|
0 ignored issues
–
show
|
|||||
| 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
|
|||||
| 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
|
|||||
| 94 | * @param null $object |
||||
|
0 ignored issues
–
show
|
|||||
| 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
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
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 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: