Skip to content

Commit a5a0647

Browse files
committed
more exception handling. first working model update.
1 parent 33351e0 commit a5a0647

20 files changed

+309
-120
lines changed

LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) :vendor_name <author@domain.com>
3+
Copyright (c) Signature Tech Studio <joseph@stechstudio.com>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+7-39
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# :package_description
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/:vendor_slug/:package_slug/run-tests?label=tests)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/:vendor_slug/:package_slug/Fix%20PHP%20code%20style%20issues?label=code%20style)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/stechstudio/laravel-hubspot.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-hubspot)
4+
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/stechstudio/laravel-hubspot/run-tests?label=tests)](https://github.com/stechstudio/laravel-hubspot/actions?query=workflow%3Arun-tests+branch%3Amain)
5+
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/stechstudio/laravel-hubspot/Fix%20PHP%20code%20style%20issues?label=code%20style)](https://github.com/stechstudio/laravel-hubspot/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/stechstudio/laravel-hubspot.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-hubspot)
77
<!--delete-->
88
---
99
This repo can be used to scaffold a Laravel package. Follow these steps to get started:
@@ -16,33 +16,18 @@ This repo can be used to scaffold a Laravel package. Follow these steps to get s
1616
<!--/delete-->
1717
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
1818

19-
## Support us
20-
21-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/:package_name.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/:package_name)
22-
23-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
24-
25-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
26-
2719
## Installation
2820

2921
You can install the package via composer:
3022

3123
```bash
32-
composer require :vendor_slug/:package_slug
33-
```
34-
35-
You can publish and run the migrations with:
36-
37-
```bash
38-
php artisan vendor:publish --tag=":package_slug-migrations"
39-
php artisan migrate
24+
composer require stechstudio/laravel-hubspot
4025
```
4126

4227
You can publish the config file with:
4328

4429
```bash
45-
php artisan vendor:publish --tag=":package_slug-config"
30+
php artisan vendor:publish --tag="laravel-hubspot-config"
4631
```
4732

4833
This is the contents of the published config file:
@@ -55,7 +40,7 @@ return [
5540
Optionally, you can publish the views using
5641

5742
```bash
58-
php artisan vendor:publish --tag=":package_slug-views"
43+
php artisan vendor:publish --tag="laravel-hubspot-views"
5944
```
6045

6146
## Usage
@@ -71,23 +56,6 @@ echo $variable->echoPhrase('Hello, VendorName!');
7156
composer test
7257
```
7358

74-
## Changelog
75-
76-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
77-
78-
## Contributing
79-
80-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
81-
82-
## Security Vulnerabilities
83-
84-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
85-
86-
## Credits
87-
88-
- [:author_name](https://github.com/:author_username)
89-
- [All Contributors](../../contributors)
90-
9159
## License
9260

9361
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

src/Api/Association.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,36 @@
22

33
namespace STS\HubSpot\Api;
44

5-
use Illuminate\Support\Collection;
65
use Illuminate\Support\Traits\ForwardsCalls;
76

87
class Association
98
{
109
use ForwardsCalls;
1110

11+
protected Collection $collection;
12+
1213
public function __construct(
1314
protected Model $object,
1415
protected array $ids = []
1516
)
16-
{}
17+
{
18+
}
1719

1820
public function get(): Collection
1921
{
20-
return $this->builder()->findMany($this->ids);
22+
if (!isset($this->collection)) {
23+
$this->collection = $this->builder()->findMany($this->ids);
24+
}
25+
26+
return $this->collection;
2127
}
2228

2329
public function builder(): Builder
2430
{
2531
return app(Builder::class)->for($this->object);
2632
}
2733

28-
public function __call($method, $parameters)
34+
public function __call($method, $parameters)
2935
{
3036
return $this->forwardCallTo($this->builder(), $method, $parameters);
3137
}

src/Api/Builder.php

+25-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class Builder
2525
protected array $with = [];
2626

2727
public function __construct(protected Client $client)
28-
{}
28+
{
29+
}
2930

3031
public function for(Model $object): static
3132
{
@@ -58,9 +59,9 @@ public function find($id, $idProperty = null): Model
5859
$response = $this->client()->get(
5960
$this->object->endpoint('read', ['id' => $id]),
6061
[
61-
'properties' => implode(",", $this->includeProperties()),
62+
'properties' => implode(",", $this->includeProperties()),
6263
'associations' => implode(",", $this->includeAssociations()),
63-
'idProperty' => $idProperty
64+
'idProperty' => $idProperty
6465
]
6566
)->json();
6667

@@ -71,11 +72,11 @@ public function findMany(array $ids, $idProperty = null): Collection
7172
{
7273
$ids = array_unique($ids);
7374

74-
if(count($ids) === 1) {
75+
if (count($ids) === 1) {
7576
return new Collection([$this->find($ids[0], $idProperty)]);
7677
}
7778

78-
if(!count($ids)) {
79+
if (!count($ids)) {
7980
return new Collection();
8081
}
8182

@@ -84,13 +85,21 @@ public function findMany(array $ids, $idProperty = null): Collection
8485
[
8586
'properties' => $this->includeProperties(),
8687
'idProperty' => $idProperty,
87-
'inputs' => array_map(fn($id) => ['id' => $id], $ids)
88+
'inputs' => array_map(fn($id) => ['id' => $id], $ids)
8889
]
8990
)->json();
9091

9192
return Collection::hydrate($response, $this->objectClass);
9293
}
9394

95+
public function update(array $properties): array
96+
{
97+
return $this->client()->patch(
98+
$this->object->endpoint('update', ['id' => $this->object->id]),
99+
['properties' => $properties]
100+
)->json();
101+
}
102+
94103
public function where($property, $condition, $value = null): static
95104
{
96105
$this->filters[] = new Filter($property, $condition, $value);
@@ -102,7 +111,7 @@ public function orderBy($property, $direction = 'ASC'): static
102111
{
103112
$this->sort = [
104113
'propertyName' => $property,
105-
'direction' => strtoupper($direction) === 'DESC' ? 'DESCENDING' : 'ASCENDING'
114+
'direction' => strtoupper($direction) === 'DESC' ? 'DESCENDING' : 'ASCENDING'
106115
];
107116

108117
return $this;
@@ -134,11 +143,11 @@ public function fetch($after = null, $limit = null): array
134143
return $this->client()->post(
135144
$this->object->endpoint('search'),
136145
[
137-
'limit' => $limit ?? $this->limit,
138-
'after' => $after ?? $this->after ?? null,
139-
'query' => $this->query ?? null,
140-
'properties' => $this->includeProperties(),
141-
'sorts' => isset($this->sort) ? [$this->sort] : null,
146+
'limit' => $limit ?? $this->limit,
147+
'after' => $after ?? $this->after ?? null,
148+
'query' => $this->query ?? null,
149+
'properties' => $this->includeProperties(),
150+
'sorts' => isset($this->sort) ? [$this->sort] : null,
142151
'filterGroups' => [[
143152
'filters' => array_map(fn($filter) => $filter->toArray(), $this->filters)
144153
]]
@@ -156,17 +165,17 @@ public function get()
156165

157166
public function cursor(): LazyCollection
158167
{
159-
return new LazyCollection(function() {
168+
return new LazyCollection(function () {
160169
$after = 0;
161170

162171
do {
163172
$response = $this->fetch($after);
164173
$after = Arr::get($response, 'paging.next.after');
165174

166-
foreach($response['results'] AS $payload) {
175+
foreach ($response['results'] as $payload) {
167176
yield new $this->objectClass($payload);
168177
}
169-
} while($after !== null);
178+
} while ($after !== null);
170179
});
171180
}
172181

@@ -181,7 +190,7 @@ public function paginate($perPage = 50, $pageName = 'page', $page = null): Lengt
181190

182191
return new LengthAwarePaginator(
183192
$results, $results->total(), $perPage, $page, [
184-
'path' => Paginator::resolveCurrentPath(),
193+
'path' => Paginator::resolveCurrentPath(),
185194
'pageName' => $pageName,
186195
]
187196
);

src/Api/Client.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Http\Client\Response;
88
use Illuminate\Support\Facades\Http;
99
use Illuminate\Support\Traits\ForwardsCalls;
10+
use STS\HubSpot\Exceptions\NotFoundException;
1011
use STS\HubSpot\Exceptions\RateLimitException;
1112

1213
/**
@@ -31,9 +32,14 @@ public function http(): PendingRequest
3132
{
3233
return Http::withToken($this->accessToken)
3334
->throw(function (Response $response, RequestException $exception) {
34-
if($response->json('category') === 'RATE_LIMITS') {
35+
if ($response->json('category') === 'RATE_LIMITS') {
3536
throw new RateLimitException($response, $exception);
3637
}
38+
if($response->status() === 404) {
39+
throw new NotFoundException($response, $exception);
40+
}
41+
42+
dd($response->status(), $response->json());
3743
});
3844
}
3945

@@ -45,11 +51,19 @@ public function get(string $uri, $query = []): Response
4551
);
4652
}
4753

48-
public function post(string $uri, $query = null): Response
54+
public function post(string $uri, array $data = []): Response
4955
{
5056
return $this->http()->post(
5157
$this->prefix($uri),
52-
array_filter($query)
58+
array_filter($data)
59+
);
60+
}
61+
62+
public function patch(string $uri, array $data = []): Response
63+
{
64+
return $this->http()->patch(
65+
$this->prefix($uri),
66+
$data
5367
);
5468
}
5569

src/Api/Concerns/HasAssociations.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ protected function getAssociationIDs($type): array
3737

3838
protected function loadAssocationIDs($type): array
3939
{
40-
return $this->builder()->associations($type);
40+
return $this->query()->associations($type);
4141
}
4242

4343
public function getAssociations($type): Collection
4444
{
45-
if($this->associationsLoaded($type)) {
45+
if ($this->associationsLoaded($type)) {
4646
return $this->associations[$type]->get();
4747
}
4848

src/Api/Filter.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct($property, $operator, $value = null, $endValue = nul
1717
$this->property = $property;
1818
$this->endValue = $endValue;
1919

20-
if($value === null && !in_array($this->translateOperator($operator), ['HAS_PROPERTY','NOT_HAS_PROPERTY'])) {
20+
if ($value === null && !in_array($this->translateOperator($operator), ['HAS_PROPERTY', 'NOT_HAS_PROPERTY'])) {
2121
$this->operator = "EQ";
2222
$this->value = $operator;
2323
} else {
@@ -28,25 +28,25 @@ public function __construct($property, $operator, $value = null, $endValue = nul
2828

2929
public function toArray()
3030
{
31-
if($this->operator === 'BETWEEN') {
31+
if ($this->operator === 'BETWEEN') {
3232
return [
3333
'propertyName' => $this->property,
34-
'operator' => $this->operator,
35-
'highValue' => $this->cast($this->value),
36-
'value' => $this->cast($this->endValue),
34+
'operator' => $this->operator,
35+
'highValue' => $this->cast($this->value),
36+
'value' => $this->cast($this->endValue),
3737
];
3838
}
3939

4040
return array_filter([
4141
'propertyName' => $this->property,
42-
'operator' => $this->operator,
43-
'value' => $this->cast($this->value)
42+
'operator' => $this->operator,
43+
'value' => $this->cast($this->value)
4444
]);
4545
}
4646

4747
protected function cast($value = null)
4848
{
49-
if($value instanceof Carbon) {
49+
if ($value instanceof Carbon) {
5050
return $value->timestamp;
5151
}
5252

@@ -56,16 +56,16 @@ protected function cast($value = null)
5656
protected function translateOperator($operator): string
5757
{
5858
return Arr::get([
59-
'=' => 'EQ',
60-
'!=' => 'NEQ',
61-
'<' => 'LT',
62-
'<=' => 'LTE',
63-
'>' => 'GT',
64-
'>=' => 'GTE',
65-
'exists' => 'HAS_PROPERTY',
59+
'=' => 'EQ',
60+
'!=' => 'NEQ',
61+
'<' => 'LT',
62+
'<=' => 'LTE',
63+
'>' => 'GT',
64+
'>=' => 'GTE',
65+
'exists' => 'HAS_PROPERTY',
6666
'not exists' => 'NOT_HAS_PROPERTY',
67-
'like' => 'CONTAINS_TOKEN',
68-
'not like' => 'NOT_CONTAINS_TOKEN'
67+
'like' => 'CONTAINS_TOKEN',
68+
'not like' => 'NOT_CONTAINS_TOKEN'
6969
], strtolower($operator), $operator);
7070
}
7171
}

0 commit comments

Comments
 (0)