-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Route all event getters through EventProvider
- Loading branch information
Showing
7 changed files
with
145 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
namespace RideTimeServer\API\Providers; | ||
|
||
use Doctrine\Common\Collections\Criteria; | ||
use RideTimeServer\API\Repositories\EventRepository; | ||
use RideTimeServer\Entities\User; | ||
use RideTimeServer\Entities\Event; | ||
use RideTimeServer\Exception\RTException; | ||
use RideTimeServer\Exception\UserException; | ||
|
||
/** | ||
* TODO: | ||
* + change list in ctrlr to use filter | ||
* - deprecate list route and use new filter in app update | ||
* + change get and filter in ctrlr to use provider | ||
* + use id in ALL relationship lists | ||
* - use provider in ctrlr to fetch related if needed | ||
* + location | ||
* - user (getRelated!) | ||
*/ | ||
class EventProvider | ||
{ | ||
/** | ||
* @var User | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* @var EventRepository | ||
*/ | ||
protected $repo; | ||
|
||
public function __construct(EventRepository $repo) { | ||
$this->repo = $repo; | ||
} | ||
|
||
public function setUser(User $user) | ||
{ | ||
$this->user = $user; | ||
} | ||
|
||
public function get(int $id) | ||
{ | ||
$this->checkUser(); | ||
|
||
/** @var Event $event */ | ||
$event = $this->repo->get($id); | ||
if (!$event->isVisible($this->user)) { | ||
throw new UserException("Event {$id} is not visible to current user", 403); | ||
} | ||
|
||
return $event; | ||
} | ||
|
||
public function filter(Criteria $criteria) | ||
{ | ||
$this->checkUser(); | ||
|
||
return $this->repo | ||
->matching($criteria) | ||
->filter(function (Event $event) { | ||
return $event->isVisible($this->user); | ||
}); | ||
} | ||
|
||
protected function checkUser() | ||
{ | ||
if (!$this->user) { | ||
throw new RTException('Cannot access events without setting user'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters