ppshobi /
psonic
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Psonic; |
||
| 4 | |||
| 5 | use Psonic\Channels\Channel; |
||
| 6 | use Psonic\Contracts\Client; |
||
| 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; |
||
| 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
Loading history...
|
|||
| 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(); |
||
| 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 |