Skip to content

Commit f322953

Browse files
authored
Add a request for events that a company is attending. (#61)
1 parent a2ccb53 commit f322953

File tree

6 files changed

+146
-6
lines changed

6 files changed

+146
-6
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace TruckersMP\APIClient\Requests\Company;
4+
5+
use Illuminate\Support\Collection;
6+
use Psr\Http\Client\ClientExceptionInterface;
7+
use TruckersMP\APIClient\Client;
8+
use TruckersMP\APIClient\Exceptions\ApiErrorException;
9+
use TruckersMP\APIClient\Models\Event;
10+
use TruckersMP\APIClient\Requests\Request;
11+
12+
class EventAttendingRequest extends Request
13+
{
14+
/**
15+
* The ID of the requested company.
16+
*
17+
* @var int
18+
*/
19+
protected int $companyId;
20+
21+
/**
22+
* Create a new EventAttendingRequest instance.
23+
*
24+
* @param Client $client
25+
* @param int $id
26+
* @return void
27+
*/
28+
public function __construct(Client $client, int $id)
29+
{
30+
parent::__construct($client);
31+
32+
$this->companyId = $id;
33+
}
34+
35+
/**
36+
* Get the endpoint of the request.
37+
*
38+
* @return string
39+
*/
40+
public function getEndpoint(): string
41+
{
42+
return 'vtc/' . $this->companyId . '/events/attending';
43+
}
44+
45+
/**
46+
* Get the data for the request.
47+
*
48+
* @return Collection|Event[]
49+
*
50+
* @throws ApiErrorException
51+
* @throws ClientExceptionInterface
52+
*/
53+
public function get(): Collection
54+
{
55+
return (new Collection($this->send()['response']))
56+
->map(fn (array $event) => new Event($this->client, $event));
57+
}
58+
}

src/Requests/Company/EventIndexRequest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,17 @@ public function get(): CompanyEventIndex
5656
$this->send()['response']
5757
);
5858
}
59+
60+
/**
61+
* Get all events that the company is attending.
62+
*
63+
* @return EventAttendingRequest
64+
*/
65+
public function attending(): EventAttendingRequest
66+
{
67+
return new EventAttendingRequest(
68+
$this->client,
69+
$this->companyId,
70+
);
71+
}
5972
}

src/Requests/CompanyRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function member(int $id): MemberRequest
149149
}
150150

151151
/**
152-
* Get the events for the company.
152+
* Get the events created by the company.
153153
*
154154
* @return EventIndexRequest
155155
*/
@@ -162,7 +162,7 @@ public function events(): EventIndexRequest
162162
}
163163

164164
/**
165-
* Get the event for the company with the specified ID.
165+
* Get the event created by the company with the specified ID.
166166
*
167167
* @param int $id
168168
* @return EventRequest

tests/Unit/Requests/CompanyEventRequestTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class CompanyEventRequestTest extends TestCase
1414

1515
public function testItCanGetAllCompanyEvents()
1616
{
17-
$this->mockRequest('company.event.index.json', 'vtc/258/events');
17+
$this->mockRequest('company.event.index.json', 'vtc/22/events');
1818

19-
$index = $this->client->company(258)->events()->get();
19+
$index = $this->client->company(22)->events()->get();
2020

2121
$this->assertInstanceOf(CompanyEventIndex::class, $index);
2222

@@ -30,10 +30,22 @@ public function testItCanGetAllCompanyEvents()
3030

3131
public function testItCanGetASpecificCompanyEvent()
3232
{
33-
$this->mockRequest('event.json', 'vtc/258/events/45');
33+
$this->mockRequest('event.json', 'vtc/22/events/45');
3434

35-
$event = $this->client->company(258)->event(45)->get();
35+
$event = $this->client->company(22)->event(45)->get();
3636

3737
$this->assertInstanceOf(Event::class, $event);
3838
}
39+
40+
public function testItCanGetEventsThatACompanyIsAttending()
41+
{
42+
$this->mockRequest('company.event.attending.json', 'vtc/22/events/attending');
43+
44+
$events = $this->client->company(22)->events()->attending()->get();
45+
46+
$this->assertInstanceOf(Collection::class, $events);
47+
$this->assertCount(1, $events);
48+
49+
$this->assertInstanceOf(Event::class, $events->first());
50+
}
3951
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"error": false,
3+
"response": [
4+
{
5+
"id": 2,
6+
"event_type": {
7+
"key": "convoy",
8+
"name": "Convoy"
9+
},
10+
"name": "Today's Convoy",
11+
"slug": "2-todays-convoy",
12+
"game": "ETS2",
13+
"server": {
14+
"id": 4,
15+
"name": "Server"
16+
},
17+
"language": "English",
18+
"departure": {
19+
"location": "Hotel",
20+
"city": "Prague"
21+
},
22+
"arrive": {
23+
"location": "Quarry",
24+
"city": "Stuttgart"
25+
},
26+
"start_at": "2023-01-04 17:00:00",
27+
"meetup_at": "2023-01-04 16:00:00",
28+
"banner": "https://static.truckersmp.com/images/event/cover/cover.png",
29+
"map": "https://static.truckersmp.com/images/event/map/map.jpeg",
30+
"description": "",
31+
"rule": null,
32+
"voice_link": null,
33+
"external_link": null,
34+
"featured": "",
35+
"vtc": {
36+
"id": 789,
37+
"name": "Example Logistics"
38+
},
39+
"user": {
40+
"id": 33,
41+
"username": "Organizer"
42+
},
43+
"attendances": {
44+
"confirmed": 11,
45+
"unsure": 5,
46+
"vtcs": 4
47+
},
48+
"dlcs": {
49+
"304212": "Scandinavia"
50+
},
51+
"url": "/events/2-todays-convoy",
52+
"created_at": "2022-01-04 17:04:58",
53+
"updated_at": "2022-01-04 17:13:15"
54+
}
55+
]
56+
}

tests/Unit/Requests/fixtures/company.event.index.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"city": "Stuttgart"
2525
},
2626
"start_at": "2023-01-04 14:32:28",
27+
"meetup_at": "2023-01-04 14:02:28",
2728
"banner": "https://static.truckersmp.com/images/event/cover/cover.png",
2829
"map": "https://static.truckersmp.com/images/event/map/map.png",
2930
"description": "Description",

0 commit comments

Comments
 (0)