Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
24 / 48 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ProductQueryBuilder | |
50.00% |
24 / 48 |
|
0.00% |
0 / 2 |
53.12 | |
0.00% |
0 / 1 |
| search | |
52.00% |
13 / 25 |
|
0.00% |
0 / 1 |
17.96 | |||
| searchSimple | |
47.83% |
11 / 23 |
|
0.00% |
0 / 1 |
17.09 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Product\QueryBuilders; |
| 4 | |
| 5 | use Domain\Product\DTOs\ProductSearchDTO; |
| 6 | use Domain\Product\DTOs\ProductSearchSimpleDTO; |
| 7 | use Illuminate\Database\Eloquent\Builder; |
| 8 | |
| 9 | class ProductQueryBuilder extends Builder |
| 10 | { |
| 11 | |
| 12 | /** |
| 13 | * @param ProductSearchDTO $productSearchDTO |
| 14 | * @return $this |
| 15 | */ |
| 16 | public function search(ProductSearchDTO $productSearchDTO): static |
| 17 | { |
| 18 | if ($productSearchDTO->description) { |
| 19 | $this->where('description', 'ilike', '%' . $productSearchDTO->description . '%'); |
| 20 | } |
| 21 | |
| 22 | $this->whereHas('integration', function (Builder $builder) use ($productSearchDTO) { |
| 23 | if ($productSearchDTO->integrationCode) { |
| 24 | $builder->where('parameters->integration_code', $productSearchDTO->integrationCode); |
| 25 | } |
| 26 | |
| 27 | if ($productSearchDTO->integrationBarcode) { |
| 28 | $builder->where('parameters->integration_barcode', $productSearchDTO->integrationBarcode); |
| 29 | } |
| 30 | |
| 31 | if ($productSearchDTO->integrationProduct) { |
| 32 | $builder->where('parameters->integration_product', $productSearchDTO->integrationProduct); |
| 33 | } |
| 34 | |
| 35 | if ($productSearchDTO->integrationDerivation) { |
| 36 | $builder->where('parameters->integration_derivation', $productSearchDTO->integrationDerivation); |
| 37 | } |
| 38 | }); |
| 39 | |
| 40 | if ($productSearchDTO->families) { |
| 41 | $this->whereHas('family', function (Builder $builder) use ($productSearchDTO) { |
| 42 | $builder->whereIn('id', $productSearchDTO->families); |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | if ($productSearchDTO->origins) { |
| 47 | $this->whereHas('family', function (Builder $familyBuilder) use ($productSearchDTO) { |
| 48 | $familyBuilder->whereHas('origin', function (Builder $originBuilder) use ($productSearchDTO) { |
| 49 | $originBuilder->whereIn('id', $productSearchDTO->origins); |
| 50 | }); |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | if ($productSearchDTO->id) { |
| 55 | $this->where('products.id', $productSearchDTO->id); |
| 56 | } |
| 57 | |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param ProductSearchSimpleDTO $productSearchDTO |
| 63 | * @return $this |
| 64 | */ |
| 65 | public function searchSimple(ProductSearchSimpleDTO $productSearchDTO): static |
| 66 | { |
| 67 | if ($productSearchDTO->description) { |
| 68 | $this->where('description', 'ilike', '%' . $productSearchDTO->description . '%'); |
| 69 | } |
| 70 | |
| 71 | $this->whereHas('integration', function (Builder $builder) use ($productSearchDTO) { |
| 72 | if ($productSearchDTO->integrationBarcode) { |
| 73 | $builder->where('parameters->integration_barcode', $productSearchDTO->integrationBarcode); |
| 74 | } |
| 75 | |
| 76 | if ($productSearchDTO->integrationProduct) { |
| 77 | $builder->where('parameters->integration_product', $productSearchDTO->integrationProduct); |
| 78 | } |
| 79 | |
| 80 | if ($productSearchDTO->integrationDerivation) { |
| 81 | $builder->where('parameters->integration_derivation', $productSearchDTO->integrationDerivation); |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | if ($productSearchDTO->families) { |
| 86 | $this->whereHas('family', function (Builder $builder) use ($productSearchDTO) { |
| 87 | $builder->whereIn('id', $productSearchDTO->families); |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | if ($productSearchDTO->origins) { |
| 92 | $this->whereHas('family', function (Builder $familyBuilder) use ($productSearchDTO) { |
| 93 | $familyBuilder->whereHas('origin', function (Builder $originBuilder) use ($productSearchDTO) { |
| 94 | $originBuilder->whereIn('id', $productSearchDTO->origins); |
| 95 | }); |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | if ($productSearchDTO->id) { |
| 100 | $this->where('products.id', $productSearchDTO->id); |
| 101 | } |
| 102 | |
| 103 | return $this; |
| 104 | } |
| 105 | } |