Skip to content

Commit

Permalink
Added Points endpoint & migrated to saloon v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Combind committed Dec 31, 2023
1 parent 2e691d0 commit 416b17d
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 5 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Mautic::segments()->addContact($segmentId, $contactId);

//Create a contact and add it to a segment
$response = Mautic::contacts()->create(strtolower($request->input('email')));
if (!$response->failed())
if ($response && !$response->failed())
{
$contactId = $response->object()->contact->id;
Mautic::segments()->addContact(4, $contactId);//4 is the segment ID, change it to your needs
Expand Down Expand Up @@ -192,6 +192,25 @@ Mautic::utmTags()->addUtmTag($contactId, $data);
Mautic::utmTags()->removeUtmTag($utmId, $contactId);
```

#### Managing Contacts points
```php
use Combindma\Mautic\Facades\Mautic;

//Add contact points
$data = array(
'eventName' => 'Score via api',
'actionName' => 'Adding',
);

Mautic::points()->addPoints($contactId, 10, $data);//$data is optional

//Subtract contact points
$data = array(
'eventname' => 'Score via api',
'actionname' => 'Subtracting',
);
Mautic::points()->subtractPoints($contactId, 10, $data);//$data is optional
```

#### Macrobale
Communicating with the API can become a repetitive process. that's why we made this package macroable.
Expand All @@ -202,7 +221,7 @@ use Combindma\Mautic\Facades\Mautic;
//include this in your macrobale file
Mautic::macro('subscribe', function(string $email) {
$response = Mautic::contacts()->create(strtolower($request->input('email')));
if (!$response->failed())
if ($response && !$response->failed())
{
$contactId = $response->object()->contact->id;
Mautic::segments()->addContact(4, $contactId);//4 is the segment ID, change it to your needs
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": "^8.1",
"guzzlehttp/guzzle": "^7.5",
"illuminate/contracts": "^10.0",
"sammyjo20/saloon": "^2.5",
"saloonphp/laravel-plugin": "^3.0",
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
Expand Down
8 changes: 7 additions & 1 deletion src/Mautic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Combindma\Mautic;

use Combindma\Mautic\Resources\ContactResource;
use Combindma\Mautic\Resources\PointsResource;
use Combindma\Mautic\Resources\SegmentResource;
use Combindma\Mautic\Resources\UtmResource;
use Combindma\Mautic\Traits\MauticStorage;
Expand All @@ -15,9 +16,9 @@

class Mautic extends Connector
{
use MauticStorage;
use AlwaysThrowOnErrors;
use Macroable;
use MauticStorage;

private readonly string $version;

Expand Down Expand Up @@ -58,6 +59,11 @@ public function utmTags(): UtmResource
return new UtmResource($this);
}

public function points(): PointsResource
{
return new PointsResource($this);
}

protected function defaultAuth(): ?Authenticator
{
if ($this->version === 'BasicAuth') {
Expand Down
2 changes: 1 addition & 1 deletion src/MauticServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function configurePackage(Package $package): void
->hasRoute('web');
}

public function packageRegistered()
public function packageRegistered(): void
{
$this->app->singleton('laravel-mautic', function () {
return new Mautic();
Expand Down
30 changes: 30 additions & 0 deletions src/Requests/AddPointsToContactRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Combindma\Mautic\Requests;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class AddPointsToContactRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(protected int $contactId, protected int $pointDelta, protected ?array $attributes = null)
{
//
}

public function resolveEndpoint(): string
{
return '/contacts/'.$this->contactId.'/points/plus/'.$this->pointDelta;
}

protected function defaultBody(): array
{
return $this->attributes ?? [];
}
}
26 changes: 26 additions & 0 deletions src/Requests/RemovePointsFromContactRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Combindma\Mautic\Requests;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class RemovePointsFromContactRequest extends Request
{
protected Method $method = Method::POST;

public function __construct(protected int $contactId, protected int $pointDelta, protected ?array $attributes = null)
{
//
}

public function resolveEndpoint(): string
{
return '/contacts/'.$this->contactId.'/points/minus/'.$this->pointDelta;
}

protected function defaultBody(): array
{
return $this->attributes ?? [];
}
}
42 changes: 42 additions & 0 deletions src/Resources/PointsResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Combindma\Mautic\Resources;

use Combindma\Mautic\Requests\AddPointsToContactRequest;
use Combindma\Mautic\Requests\RemovePointsFromContactRequest;
use Exception;
use Illuminate\Support\Facades\Log;
use Saloon\Http\Response;

class PointsResource extends Resource
{
public function addPoints(int $contactId, int $pointDelta, ?array $attributes = null): ?Response
{
if ($this->connector->isApiDisabled()) {
return null;
}

try {
return $this->connector->send(new AddPointsToContactRequest($contactId, $pointDelta, $attributes));
} catch (Exception $e) {
Log::error($e);
}

return null;
}

public function subtractPoints(int $contactId, int $pointDelta, ?array $attributes = null): ?Response
{
if ($this->connector->isApiDisabled()) {
return null;
}

try {
return $this->connector->send(new RemovePointsFromContactRequest($contactId, $pointDelta, $attributes));
} catch (Exception $e) {
Log::error($e);
}

return null;
}
}

0 comments on commit 416b17d

Please sign in to comment.