tzurbaev /
osnova-api
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Osnova\Services\Timeline; |
||
| 4 | |||
| 5 | use GuzzleHttp\Exception\RequestException; |
||
| 6 | use Osnova\OsnovaResource; |
||
| 7 | use Osnova\Services\AbstractService; |
||
| 8 | use Osnova\Services\Timeline\Interfaces\ModifiesTimelineRequest; |
||
| 9 | use Osnova\Services\Timeline\Interfaces\TimelineOwnerInterface; |
||
| 10 | use Osnova\Services\Timeline\Owners\TimelineSearch; |
||
| 11 | use Osnova\Services\Timeline\Requests\TimelineRequest; |
||
| 12 | use Osnova\Services\Timeline\Requests\TimelineSearchRequest; |
||
| 13 | |||
| 14 | class Timeline extends AbstractService |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Get timeline entries list. |
||
| 18 | * |
||
| 19 | * @param TimelineOwnerInterface $owner Timeline owner. |
||
| 20 | * @param TimelineRequest $request Timeline request parameters. |
||
| 21 | * @param OsnovaResource $resource = null Osnova resource instance that will be bound to entries. |
||
| 22 | * |
||
| 23 | * @return array|Entry[] |
||
| 24 | */ |
||
| 25 | public function getTimeline(TimelineOwnerInterface $owner, TimelineRequest $request, OsnovaResource $resource = null) |
||
| 26 | { |
||
| 27 | try { |
||
| 28 | $resolvedRequest = $this->resolveRequest($owner, $request); |
||
| 29 | $response = $this->getApiProvider()->getClient()->request( |
||
| 30 | 'GET', |
||
| 31 | $this->getTimelineUrl($owner, $resolvedRequest), |
||
| 32 | ['query' => $resolvedRequest->getParams()] |
||
| 33 | ); |
||
| 34 | |||
| 35 | return $this->getEntitiesBuilder(Entry::class) |
||
| 36 | ->fromResponse($response) |
||
| 37 | ->with($this->getApiProvider(), $resource) |
||
| 38 | ->collection(); |
||
| 39 | } catch (RequestException $e) { |
||
| 40 | // |
||
| 41 | } |
||
| 42 | |||
| 43 | return []; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get timeline search results. |
||
| 48 | * |
||
| 49 | * @param string $query Query string. |
||
| 50 | * @param string $orderBy Ordering method (`relevant` or `date`). |
||
| 51 | * @param int $page = 1 Results page. |
||
| 52 | * @param OsnovaResource $resource = null Osnova resource instance that will be bound to entries. |
||
| 53 | * |
||
| 54 | * @return array|Entry[] |
||
| 55 | */ |
||
| 56 | public function getTimelineSearchResults(string $query, string $orderBy = 'relevant', int $page = 1, OsnovaResource $resource = null) |
||
| 57 | { |
||
| 58 | return $this->getTimeline( |
||
| 59 | new TimelineSearch($query), |
||
| 60 | new TimelineSearchRequest($orderBy, $page), |
||
| 61 | $resource |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get timeline entry by ID. |
||
| 67 | * |
||
| 68 | * @param int $id Entry ID. |
||
| 69 | * @param OsnovaResource $resource = null Osnova resource that will be bound to entry. |
||
| 70 | * |
||
| 71 | * @return Entry|null |
||
| 72 | */ |
||
| 73 | public function getTimelineEntry($id, OsnovaResource $resource = null) |
||
| 74 | { |
||
| 75 | try { |
||
| 76 | $response = $this->getApiProvider()->getClient()->request('GET', 'entries/'.$id); |
||
| 77 | |||
| 78 | return $this->getEntitiesBuilder(Entry::class) |
||
| 79 | ->fromResponse($response) |
||
| 80 | ->with($this->getApiProvider(), $resource) |
||
| 81 | ->item(); |
||
| 82 | } catch (RequestException $e) { |
||
| 83 | // |
||
| 84 | } |
||
| 85 | |||
| 86 | return null; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Resolve correct timeline request to be used in API request. |
||
| 91 | * |
||
| 92 | * @param TimelineOwnerInterface|ModifiesTimelineRequest $owner |
||
| 93 | * @param TimelineRequest $request |
||
| 94 | * |
||
| 95 | * @return TimelineRequest |
||
| 96 | */ |
||
| 97 | protected function resolveRequest(TimelineOwnerInterface $owner, TimelineRequest $request) |
||
| 98 | { |
||
| 99 | if (!($owner instanceof ModifiesTimelineRequest)) { |
||
| 100 | return $request; |
||
| 101 | } |
||
| 102 | |||
| 103 | $modified = $owner->modifyTimelineRequest($request); |
||
| 104 | |||
| 105 | return $modified instanceof TimelineRequest ? $modified : $request; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Build timeline URL for the given owner & request. |
||
| 110 | * |
||
| 111 | * @param TimelineOwnerInterface $owner Timeline owner. |
||
| 112 | * @param TimelineRequest $request Timeline request parameters. |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getTimelineUrl(TimelineOwnerInterface $owner, TimelineRequest $request) |
||
| 117 | { |
||
| 118 | $prefix = trim($owner->getTimelineUrlPrefix(), '/'); |
||
| 119 | $sorting = trim($request->getSorting(), '/'); |
||
| 120 | |||
| 121 | return $prefix.($sorting ? '/'.$sorting : ''); |
||
| 122 | } |
||
| 123 | } |
||
| 124 |