|
| 1 | +<?php |
| 2 | +namespace Tqdev\PhpCrudApi\Middleware; |
| 3 | + |
| 4 | +use Tqdev\PhpCrudApi\Column\ReflectionService; |
| 5 | +use Tqdev\PhpCrudApi\Controller\Responder; |
| 6 | +use Tqdev\PhpCrudApi\Middleware\Base\Middleware; |
| 7 | +use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore; |
| 8 | +use Tqdev\PhpCrudApi\Middleware\Router\Router; |
| 9 | +use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition; |
| 10 | +use Tqdev\PhpCrudApi\Record\Condition\Condition; |
| 11 | +use Tqdev\PhpCrudApi\Record\Condition\NoCondition; |
| 12 | +use Tqdev\PhpCrudApi\Record\RequestUtils; |
| 13 | +use Tqdev\PhpCrudApi\Request; |
| 14 | +use Tqdev\PhpCrudApi\Response; |
| 15 | + |
| 16 | +class MultiTenancyMiddleware extends Middleware |
| 17 | +{ |
| 18 | + private $reflection; |
| 19 | + |
| 20 | + public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection) |
| 21 | + { |
| 22 | + parent::__construct($router, $responder, $properties); |
| 23 | + $this->reflection = $reflection; |
| 24 | + $this->utils = new RequestUtils($reflection); |
| 25 | + } |
| 26 | + |
| 27 | + private function getCondition(String $tableName, array $pairs): Condition |
| 28 | + { |
| 29 | + $condition = new NoCondition(); |
| 30 | + $table = $this->reflection->getTable($tableName); |
| 31 | + foreach ($pairs as $k => $v) { |
| 32 | + $condition = $condition->_and(new ColumnCondition($table->get($k), 'eq', $v)); |
| 33 | + } |
| 34 | + return $condition; |
| 35 | + } |
| 36 | + |
| 37 | + private function getPairs($handler, String $operation, String $tableName): array |
| 38 | + { |
| 39 | + $result = array(); |
| 40 | + $pairs = call_user_func($handler, $operation, $tableName); |
| 41 | + $table = $this->reflection->getTable($tableName); |
| 42 | + foreach ($pairs as $k => $v) { |
| 43 | + if ($table->exists($k)) { |
| 44 | + $result[$k] = $v; |
| 45 | + } |
| 46 | + } |
| 47 | + return $result; |
| 48 | + } |
| 49 | + |
| 50 | + private function handleRecord(Request $request, String $operation, array $pairs) /*: void*/ |
| 51 | + { |
| 52 | + $record = $request->getBody(); |
| 53 | + if ($record === null) { |
| 54 | + return; |
| 55 | + } |
| 56 | + $multi = is_array($record); |
| 57 | + $records = $multi ? $record : [$record]; |
| 58 | + foreach ($records as &$record) { |
| 59 | + foreach ($pairs as $column => $value) { |
| 60 | + if ($operation == 'create') { |
| 61 | + $record->$column = $value; |
| 62 | + } else { |
| 63 | + if (isset($record->$column)) { |
| 64 | + unset($record->$column); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + $request->setBody($multi ? $records : $records[0]); |
| 70 | + } |
| 71 | + |
| 72 | + public function handle(Request $request): Response |
| 73 | + { |
| 74 | + $handler = $this->getProperty('handler', ''); |
| 75 | + if ($handler !== '') { |
| 76 | + $path = $request->getPathSegment(1); |
| 77 | + if ($path == 'records') { |
| 78 | + $operation = $this->utils->getOperation($request); |
| 79 | + $tableNames = $this->utils->getTableNames($request); |
| 80 | + foreach ($tableNames as $i => $tableName) { |
| 81 | + if (!$this->reflection->hasTable($tableName)) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + $pairs = $this->getPairs($handler, $operation, $tableName); |
| 85 | + if ($i == 0) { |
| 86 | + if (in_array($operation, ['create', 'update', 'increment'])) { |
| 87 | + $this->handleRecord($request, $operation, $pairs); |
| 88 | + } |
| 89 | + } |
| 90 | + $condition = $this->getCondition($tableName, $pairs); |
| 91 | + VariableStore::set("multiTenancy.conditions.$tableName", $condition); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return $this->next->handle($request); |
| 96 | + } |
| 97 | +} |
0 commit comments