Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 153fcb2

Browse files
authored
Merge pull request #13 from Saritasa/feature/code-optimization
Improve inside logic of repository
2 parents 8ec62d9 + 7358fdf commit 153fcb2

17 files changed

Lines changed: 1010 additions & 590 deletions

CHANGES.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changes History
22

3+
3.3.0
4+
------
5+
+ Count method now can receive fieldValues array to filter results
6+
+ Repository methods create/save/delete/ now can throw an exception if given model type don't match with served
7+
model by repository
8+
+ Repository findOrFail method now checks compliance types of given value and served model primary key.
9+
+ getPage and getCursorPage methods now use nested queries functionality as well as findWhere/getWhere/getWith
10+
+ Method getWithBuilder marked as deprecated
11+
+ Increased unit tests coverage
12+
+ Fixed small description issues
13+
314
3.2.1
415
------
516
+ Methods getWith/findWhere/getWhere call when as value uses Carbon\Carbon instance and as key field name, now not throws BadCriteriaException.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"require-dev": {
2020
"mockery/mockery": "^1.1",
2121
"phpunit/phpunit": "^7.5",
22-
"squizlabs/php_codesniffer": "^3.0"
22+
"squizlabs/php_codesniffer": "^3.0",
23+
"slevomat/coding-standard": "^5.0"
2324
},
2425
"prefer-stable" : true,
2526
"minimum-stability": "alpha",

phpcs.xml

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<description>The coding standard for laravel package</description>
44

55
<file>src</file>
6+
<file>tests</file>
67

78
<exclude-pattern>*.json</exclude-pattern>
89
<exclude-pattern>*.xml</exclude-pattern>
@@ -25,13 +26,52 @@
2526
<rule ref="Generic.CodeAnalysis.JumbledIncrementer" />
2627
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement" />
2728
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier" />
28-
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter">
29-
<exclude-pattern>src/Scopes/SortByName.php</exclude-pattern>
30-
</rule>
29+
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
3130
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod" />
3231
<rule ref="Generic.Commenting.Fixme" />
3332
<rule ref="Generic.Commenting.Todo" />
3433
<rule ref="Generic.ControlStructures.InlineControlStructure" />
34+
35+
<!-- Slevomat additioanl rules -->
36+
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Operators/DisallowEqualOperatorsSniff.php"/>
37+
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Arrays/TrailingArrayCommaSniff.php"/>
38+
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/TraitUseDeclarationSniff.php"/>
39+
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/TraitUseSpacingSniff.php">
40+
<properties>
41+
<property name="linesCountBeforeFirstUse" value="0"/>
42+
<property name="linesCountAfterLastUseWhenLastInClass" value="0"/>
43+
</properties>
44+
</rule>
45+
46+
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php">
47+
<properties>
48+
<property name="linesCountBeforeFirstContent" value="0"/>
49+
<property name="linesCountBetweenDescriptionAndAnnotations" value="1"/>
50+
<property name="linesCountBetweenDifferentAnnotationsTypes" value="1"/>
51+
<property name="linesCountAfterLastContent" value="0"/>
52+
</properties>
53+
</rule>
54+
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Commenting/EmptyCommentSniff.php"/>
55+
<rule
56+
ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/NewWithParenthesesSniff.php"/>
57+
<rule
58+
ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php"/>
59+
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php">
60+
<properties>
61+
<property name="searchAnnotations" value="1"/>
62+
</properties>
63+
</rule>
64+
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php">
65+
<properties>
66+
<property name="searchAnnotations" value="1"/>
67+
</properties>
68+
</rule>
69+
<rule
70+
ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/UseDoesNotStartWithBackslashSniff.php"/>
71+
<rule
72+
ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/TypeHints/NullableTypeForNullDefaultValueSniff.php"/>
73+
<rule
74+
ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/TypeHints/NullTypeHintOnLastPositionSniff.php"/>
3575
</ruleset>
3676

3777

src/Contracts/IRepository.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Support\Collection;
8+
use InvalidArgumentException;
89
use Saritasa\DingoApi\Paging\CursorRequest;
910
use Saritasa\DingoApi\Paging\CursorResult;
1011
use Saritasa\DingoApi\Paging\PagingInfo;
1112
use Saritasa\LaravelRepositories\DTO\SortOptions;
13+
use Saritasa\LaravelRepositories\Exceptions\BadCriteriaException;
1214
use Saritasa\LaravelRepositories\Exceptions\ModelNotFoundException;
1315
use Saritasa\LaravelRepositories\Exceptions\RepositoryException;
1416

@@ -39,21 +41,26 @@ public function getModelValidationRules(): array;
3941
* @return Model
4042
*
4143
* @throws ModelNotFoundException
44+
* @throws RepositoryException
4245
*/
4346
public function findOrFail($id): Model;
4447

4548
/**
4649
* Returns first model matching given filters.
4750
*
4851
* @param array $fieldValues Filters collection
52+
*
4953
* @return Model|null
54+
*
55+
* @throws BadCriteriaException
5056
*/
5157
public function findWhere(array $fieldValues): ?Model;
5258

5359
/**
5460
* Create model in storage.
5561
*
5662
* @param Model $entity Model to create
63+
*
5764
* @return Model
5865
*
5966
* @throws RepositoryException
@@ -75,6 +82,7 @@ public function save(Model $entity): Model;
7582
* Delete model in storage.
7683
*
7784
* @param Model $entity Model to delete
85+
*
7886
* @return void
7987
*
8088
* @throws RepositoryException
@@ -84,7 +92,7 @@ public function delete(Model $entity): void;
8492
/**
8593
* Returns models list.
8694
*
87-
* @return Collection
95+
* @return Collection|Model[]
8896
*/
8997
public function get(): Collection;
9098

@@ -93,27 +101,34 @@ public function get(): Collection;
93101
*
94102
* @param array $fieldValues Filters collection
95103
*
96-
* @return Collection
104+
* @return Collection|Model[]
105+
*
106+
* @throws BadCriteriaException
97107
*/
98108
public function getWhere(array $fieldValues): Collection;
99109

100110
/**
101111
* Get models collection as pagination.
102112
*
103113
* @param PagingInfo $paging Paging information
104-
* @param array|null $fieldValues Filters collection
114+
* @param array $fieldValues Filters collection
105115
*
106116
* @return LengthAwarePaginator
117+
*
118+
* @throws BadCriteriaException
119+
* @throws InvalidArgumentException
107120
*/
108121
public function getPage(PagingInfo $paging, array $fieldValues = []): LengthAwarePaginator;
109122

110123
/**
111124
* Get models collection as cursor.
112125
*
113126
* @param CursorRequest $cursor Request with cursor data
114-
* @param array|null $fieldValues Filters collection
127+
* @param array $fieldValues Filters collection
115128
*
116129
* @return CursorResult
130+
*
131+
* @throws BadCriteriaException
117132
*/
118133
public function getCursorPage(CursorRequest $cursor, array $fieldValues = []): CursorResult;
119134

@@ -123,29 +138,35 @@ public function getCursorPage(CursorRequest $cursor, array $fieldValues = []): C
123138
* @param array $with Which relations should be preloaded
124139
* @param array $withCounts Which related entities should be counted
125140
* @param array $fieldValues Conditions that retrieved entities should satisfy
126-
* @param SortOptions $sortOptions How list of items should be sorted
141+
* @param SortOptions|null $sortOptions How list of items should be sorted
127142
*
128-
* @return Collection
143+
* @return Collection|Model[]
144+
*
145+
* @throws BadCriteriaException
129146
*/
130147
public function getWith(
131148
array $with,
132149
array $withCounts = [],
133150
array $fieldValues = [],
134-
SortOptions $sortOptions = null
151+
?SortOptions $sortOptions = null
135152
): Collection;
136153

137154
/**
138155
* Return entities count.
139156
*
157+
* @param array $fieldValues Conditions that retrieved entities should satisfy
158+
*
140159
* @return integer
160+
*
161+
* @throws BadCriteriaException
141162
*/
142-
public function count(): int;
163+
public function count(array $fieldValues = []): int;
143164

144165
/**
145166
* List of fields, allowed to use in the search.
146167
* Should be determine in the inheritors. Determines the result of the list request of entities.
147168
*
148-
* @return array
169+
* @return array
149170
*/
150171
public function getSearchableFields(): array;
151172
}

src/DTO/SortOptions.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Saritasa\LaravelRepositories\DTO;
44

55
use Saritasa\Dto;
6-
use Saritasa\LaravelRepositories\Enums\OrderDirections;
76
use Saritasa\Exceptions\InvalidEnumValueException;
7+
use Saritasa\LaravelRepositories\Enums\OrderDirections;
88

99
/**
1010
* The model contains the sort order direction and the field to sort by.
@@ -14,15 +14,15 @@ class SortOptions extends Dto
1414
/**
1515
* Order by attribute name.
1616
*/
17-
const ORDER_BY = 'orderBy';
17+
public const ORDER_BY = 'orderBy';
1818

1919
/**
2020
* Sort order attribute name;
2121
*/
22-
const SORT_ORDER = 'sortOrder';
22+
public const SORT_ORDER = 'sortOrder';
2323

2424
/**
25-
* Create SortOptionsData model.
25+
* The model contains the sort order direction and the field to sort by.
2626
*
2727
* @param string $orderBy Field name to sort records by
2828
* @param string $sortOrder Sort order direction (asc, desc)
@@ -48,6 +48,7 @@ public function __construct(string $orderBy, string $sortOrder = OrderDirections
4848
* Sort order direction.
4949
*
5050
* @see OrderDirections
51+
*
5152
* @var string
5253
*/
5354
public $sortOrder;

src/Exceptions/RepositoryRegisterException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
*/
1010
class RepositoryRegisterException extends Exception
1111
{
12-
1312
}

src/LaravelRepositoriesServiceProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Saritasa\LaravelRepositories;
44

5+
use Illuminate\Contracts\Container\BindingResolutionException;
56
use Illuminate\Support\ServiceProvider;
6-
use Saritasa\LaravelRepositories\Repositories\RepositoryFactory;
77
use Saritasa\LaravelRepositories\Contracts\IRepositoryFactory;
8+
use Saritasa\LaravelRepositories\Repositories\RepositoryFactory;
89

910
/**
1011
* Package providers. Registers package implementation in DI container.
@@ -26,6 +27,8 @@ public function register(): void
2627
* Make package settings needed to correct work.
2728
*
2829
* @return void
30+
*
31+
* @throws BindingResolutionException
2932
*/
3033
public function boot(): void
3134
{
@@ -45,6 +48,8 @@ public function boot(): void
4548
* Register custom repositories implementations.
4649
*
4750
* @return void
51+
*
52+
* @throws BindingResolutionException
4853
*/
4954
protected function registerCustomBindings(): void
5055
{

src/Repositories/CachingRepository.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Support\Collection;
10+
use Psr\SimpleCache\InvalidArgumentException;
1011
use Saritasa\DingoApi\Paging\CursorRequest;
1112
use Saritasa\DingoApi\Paging\CursorResult;
1213
use Saritasa\DingoApi\Paging\PagingInfo;
14+
use Saritasa\LaravelRepositories\Contracts\IRepository;
1315
use Saritasa\LaravelRepositories\DTO\SortOptions;
1416
use Saritasa\LaravelRepositories\Exceptions\ModelNotFoundException;
1517
use Saritasa\LaravelRepositories\Exceptions\RepositoryException;
16-
use Saritasa\LaravelRepositories\Contracts\IRepository;
1718

1819
/**
1920
* Wrapper for repositories to store data into cache.
@@ -194,6 +195,8 @@ public function getCursorPage(CursorRequest $cursor, array $fieldValues = []): C
194195
* @param string|int id Id to find
195196
*
196197
* @return Model|null
198+
*
199+
* @throws RepositoryException
197200
*/
198201
private function find($id): ?Model
199202
{
@@ -224,6 +227,8 @@ protected function cachedFind($id): ?Model
224227
* @param Model $model Model to invalidate
225228
*
226229
* @return void
230+
*
231+
* @throws InvalidArgumentException
227232
*/
228233
protected function invalidate(Model $model): void
229234
{
@@ -296,11 +301,11 @@ public function getWith(
296301
/**
297302
* {@inheritdoc}
298303
*/
299-
public function count(): int
304+
public function count(array $fieldValues = []): int
300305
{
301306
$key = $this->prefix . ":all:count";
302-
return $this->cached($key, function () {
303-
return $this->repository->count();
307+
return $this->cached($key, function () use ($fieldValues) {
308+
return $this->repository->count($fieldValues);
304309
});
305310
}
306311

0 commit comments

Comments
 (0)