Skip to content

Commit 33351e0

Browse files
committed
loading engagements
1 parent 665ced9 commit 33351e0

File tree

11 files changed

+123
-66
lines changed

11 files changed

+123
-66
lines changed

config/hubspot.php

+15
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,20 @@
3535
'quotes' => [
3636
'include_properties' => [],
3737
'include_associations' => ['companies','contacts','deals','tickets'],
38+
],
39+
40+
'calls' => [
41+
'include_properties' => ['hs_call_title','hubspot_owner_id','hs_call_body','hs_call_direction','hs_call_callee_object_id','hs_call_callee_object_type_id','hs_call_disposition','hs_call_duration','hs_call_from_number','hs_call_to_number'],
42+
'include_associations' => ['companies','contacts','deals','tickets'],
43+
],
44+
45+
'emails' => [
46+
'include_properties' => ['hubspot_owner_id','hs_timestamp','hs_email_subject','hs_email_status','hs_email_text','hs_email_direction'],
47+
'include_associations' => ['companies','contacts','deals','tickets'],
48+
],
49+
50+
'notes' => [
51+
'include_properties' => ['hubspot_owner_id','hs_timestamp','hs_note_body'],
52+
'include_associations' => ['companies','contacts','deals','tickets'],
3853
]
3954
];

src/Api/Association.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Association
1111

1212
public function __construct(
1313
protected Model $object,
14-
protected array $ids
14+
protected array $ids = []
1515
)
1616
{}
1717

src/Api/Builder.php

+28-15
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Builder
2222
protected string $objectClass;
2323

2424
protected array $properties = [];
25-
protected array $associations = [];
25+
protected array $with = [];
2626

2727
public function __construct(protected Client $client)
2828
{}
@@ -46,7 +46,7 @@ public function include($properties): static
4646

4747
public function with($associations): static
4848
{
49-
$this->associations = is_array($associations)
49+
$this->with = is_array($associations)
5050
? $associations
5151
: func_get_args();
5252

@@ -55,16 +55,16 @@ public function with($associations): static
5555

5656
public function find($id, $idProperty = null): Model
5757
{
58-
$response = $this->client->get(
58+
$response = $this->client()->get(
5959
$this->object->endpoint('read', ['id' => $id]),
6060
[
61-
'properties' => implode(",", $this->properties()),
62-
'associations' => implode(",", $this->associations()),
61+
'properties' => implode(",", $this->includeProperties()),
62+
'associations' => implode(",", $this->includeAssociations()),
6363
'idProperty' => $idProperty
6464
]
6565
)->json();
6666

67-
return new $this->objectClass($response);
67+
return (new $this->objectClass($response))->has($this->includeAssociations());
6868
}
6969

7070
public function findMany(array $ids, $idProperty = null): Collection
@@ -79,10 +79,10 @@ public function findMany(array $ids, $idProperty = null): Collection
7979
return new Collection();
8080
}
8181

82-
$response = $this->client->post(
82+
$response = $this->client()->post(
8383
$this->object->endpoint('batchRead'),
8484
[
85-
'properties' => $this->properties(),
85+
'properties' => $this->includeProperties(),
8686
'idProperty' => $idProperty,
8787
'inputs' => array_map(fn($id) => ['id' => $id], $ids)
8888
]
@@ -131,13 +131,13 @@ public function search($input): static
131131

132132
public function fetch($after = null, $limit = null): array
133133
{
134-
return $this->client->post(
134+
return $this->client()->post(
135135
$this->object->endpoint('search'),
136136
[
137137
'limit' => $limit ?? $this->limit,
138138
'after' => $after ?? $this->after ?? null,
139139
'query' => $this->query ?? null,
140-
'properties' => $this->properties(),
140+
'properties' => $this->includeProperties(),
141141
'sorts' => isset($this->sort) ? [$this->sort] : null,
142142
'filterGroups' => [[
143143
'filters' => array_map(fn($filter) => $filter->toArray(), $this->filters)
@@ -192,19 +192,32 @@ public function count(): int
192192
return Arr::get($this->get(1, 0, false), 'total', 0);
193193
}
194194

195-
protected function properties(): array
195+
public function client(): Client
196+
{
197+
return $this->client;
198+
}
199+
200+
public function associations($association): array
201+
{
202+
return $this->client()->get(
203+
$this->object->endpoint('associations', ['id' => $this->object->id, 'association' => $association])
204+
)->json()['results'];
205+
}
206+
207+
protected function includeProperties(): array
196208
{
197209
return array_merge(
198-
config("hubspot.{$this->object->type()}.include_properties"),
210+
config("hubspot.{$this->object->type()}.include_properties", []),
199211
$this->properties
200212
);
201213
}
202214

203-
protected function associations(): array
215+
protected function includeAssociations(): array
204216
{
205217
return array_merge(
206-
config("hubspot.{$this->object->type()}.include_associations"),
207-
$this->associations
218+
config("hubspot.{$this->object->type()}.include_associations", []),
219+
$this->with
208220
);
209221
}
222+
210223
}

src/Api/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function http(): PendingRequest
3737
});
3838
}
3939

40-
public function get(string $uri, $query = null): Response
40+
public function get(string $uri, $query = []): Response
4141
{
4242
return $this->http()->get(
4343
$this->prefix($uri),

src/Api/Concerns/HasAssociations.php

+21-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
trait HasAssociations
1010
{
1111
protected array $associations;
12+
protected array $preloaded = [];
1213

13-
public function associations($type): Association
14+
public function has(array $preloaded): static
1415
{
15-
if(!array_key_exists('associations', $this->payload)) {
16-
$this->payload['associations'] = $this->builder()->find($this->id)->get('associations');
17-
}
16+
$this->preloaded = $preloaded;
1817

18+
return $this;
19+
}
20+
21+
public function associations($type): Association
22+
{
1923
return new Association(
2024
self::factory($type),
2125
$this->getAssociationIDs($type)
@@ -24,15 +28,22 @@ public function associations($type): Association
2428

2529
protected function getAssociationIDs($type): array
2630
{
27-
return Arr::pluck(
28-
Arr::get($this->payload, "associations.$type.results", [])
29-
, 'id');
31+
$results = array_key_exists($type, $this->preloaded)
32+
? Arr::get($this->payload, "associations.$type.results", [])
33+
: $this->loadAssocationIDs($type);
34+
35+
return Arr::pluck($results, 'id');
36+
}
37+
38+
protected function loadAssocationIDs($type): array
39+
{
40+
return $this->builder()->associations($type);
3041
}
3142

3243
public function getAssociations($type): Collection
3344
{
3445
if($this->associationsLoaded($type)) {
35-
return $this->associations[$type];
46+
return $this->associations[$type]->get();
3647
}
3748

3849
return $this->loadAssociations($type);
@@ -45,8 +56,8 @@ public function associationsLoaded($type): bool
4556

4657
public function loadAssociations($type): Collection
4758
{
48-
$this->associations[$type] = $this->associations($type)->get();
59+
$this->associations[$type] = $this->associations($type);
4960

50-
return $this->associations[$type];
61+
return $this->associations[$type]->get();
5162
}
5263
}

src/Api/Model.php

+27-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,27 @@ abstract class Model
1515

1616
protected string $type;
1717

18-
protected array $endpoints = [];
19-
protected array $schema = [];
20-
2118
protected array $payload = [];
2219
protected array $properties = [];
2320

21+
protected array $schema = [
22+
'id' => 'int',
23+
'properties' => 'array',
24+
'propertiesWithHistory' => 'array',
25+
'associations' => 'array',
26+
'createdAt' => 'datetime',
27+
'updatedAt' => 'datetime',
28+
'archived' => 'bool',
29+
'archivedAt' => 'datetime',
30+
];
31+
32+
protected array $endpoints = [
33+
"read" => "/v3/objects/{type}/{id}",
34+
"batchRead" => "/v3/objects/{type}/batch/read",
35+
"search" => "/v3/objects/{type}/search",
36+
"associations" => "/v3/objects/{type}/{id}/associations/{association}",
37+
];
38+
2439
public function __construct(array $payload = [])
2540
{
2641
$this->payload = $payload;
@@ -39,12 +54,19 @@ public function type(): string
3954
return $this->type;
4055
}
4156

57+
protected function endpoints(): array
58+
{
59+
return $this->endpoints;
60+
}
61+
4262
public function endpoint($key, $fill = []): string
4363
{
64+
$fill['type'] = $this->type;
65+
4466
return str_replace(
4567
array_map(fn($key) => "{" . $key . "}", array_keys($fill)),
4668
array_values($fill),
47-
$this->endpoints[$key]
69+
$this->endpoints()[$key]
4870
);
4971
}
5072

@@ -55,7 +77,7 @@ public function builder(): Builder
5577

5678
public function __get($key)
5779
{
58-
if(in_array($key, ['contacts','companies','deals','tickets'])) {
80+
if(in_array($key, ['contacts','companies','deals','tickets','notes','calls','emails','meetings','tasks'])) {
5981
return $this->getAssociations($key);
6082
}
6183

src/Crm/Call.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace STS\HubSpot\Crm;
4+
5+
use STS\HubSpot\Api\Model;
6+
7+
class Call extends Model
8+
{
9+
protected string $type = "calls";
10+
}

src/Crm/Company.php

-17
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,4 @@
77
class Company extends Model
88
{
99
protected string $type = "companies";
10-
11-
protected array $endpoints = [
12-
"read" => "/v3/objects/companies/{id}",
13-
"batchRead" => "/v3/objects/companies/batch/read",
14-
"search" => "/v3/objects/companies/search",
15-
];
16-
17-
protected array $schema = [
18-
'id' => 'int',
19-
'properties' => 'array',
20-
'propertiesWithHistory' => 'array',
21-
'associations' => 'array',
22-
'createdAt' => 'datetime',
23-
'updatedAt' => 'datetime',
24-
'archived' => 'bool',
25-
'archivedAt' => 'datetime',
26-
];
2710
}

src/Crm/Contact.php

-17
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,4 @@
77
class Contact extends Model
88
{
99
protected string $type = "contacts";
10-
11-
protected array $endpoints = [
12-
"read" => "/v3/objects/contacts/{id}",
13-
"batchRead" => "/v3/objects/contacts/batch/read",
14-
"search" => "/v3/objects/contacts/search",
15-
];
16-
17-
protected array $schema = [
18-
'id' => 'int',
19-
'properties' => 'array',
20-
'propertiesWithHistory' => 'array',
21-
'associations' => 'array',
22-
'createdAt' => 'datetime',
23-
'updatedAt' => 'datetime',
24-
'archived' => 'bool',
25-
'archivedAt' => 'datetime',
26-
];
2710
}

src/Crm/Email.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace STS\HubSpot\Crm;
4+
5+
use STS\HubSpot\Api\Model;
6+
7+
class Email extends Model
8+
{
9+
protected string $type = "emails";
10+
}

src/Crm/Note.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace STS\HubSpot\Crm;
4+
5+
use STS\HubSpot\Api\Model;
6+
7+
class Note extends Model
8+
{
9+
protected string $type = "notes";
10+
}

0 commit comments

Comments
 (0)