Skip to content

Commit 9802fa0

Browse files
authored
Merge pull request #9 from wazzac/develop/v1.2.0
Develop/v1.2.0 to `main`: New version release - added auto sync trait; renames a few files and updated documentation.
2 parents 73789e8 + db66bba commit 9802fa0

File tree

12 files changed

+496
-416
lines changed

12 files changed

+496
-416
lines changed

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
# Release Notes
22

3+
## v1.2.0 `2025-06-17`
4+
5+
### Added
6+
7+
- Introduced `ShouldSyncToCrmOnSave` to automatically initiate CRM sync after model save.
8+
9+
### Changed
10+
11+
- Renamed `CrmTrait` to `HasCrmSync` in `namespace Wazza\SyncModelToCrm\Traits;`.
12+
- Renamed `CrmController` to `CrmSyncController` in `namespace Wazza\SyncModelToCrm\Http\Controllers;`.
13+
- Enhanced inline documentation in both trait files to better explain their use cases.
14+
- Updated code to use the singleton instance for CRM sync instead of creating new instances. e.g. `app(CrmSyncController::class)` instead of `new CrmSyncController()`
15+
16+
### Important changes from v1.1.0
17+
- Bulk update `CrmTrait` to `HasCrmSync` for all `Wazza\SyncModelToCrm\Traits\HasCrmSync` used files.
18+
319
## v1.1.0 `2025-06-04`
420

521
### Stable Release 🚀
622

723
Our first stable release. The package has been in beta for a few months without any major issues. We will keep on improving the codebase and continue to add more sync functionality.
824

925
- Added the ability for the `smtc_external_key_lookup` mapping table to support both `uuid` and `int` foreign keys.
10-
- Fixed a bug in the `crmTrait` file where the Model instance was not given but the Model name (string).
26+
- Fixed a bug in the `HasCrmSync` file where the Model instance was not given but the Model name (string).
1127
- Fixed PHP class types.
1228
- Documentation updates.
1329

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Warren Coetzee
3+
Copyright (c) 2025 Warren Coetzee
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

Lines changed: 49 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ use Laravel\Sanctum\HasApiTokens;
7777
use App\Models\Entity;
7878
use Illuminate\Database\Eloquent\SoftDeletes;
7979
use Wazza\SyncModelToCrm\Http\Controllers\CrmProviders\HubSpotController;
80-
use Wazza\SyncModelToCrm\Traits\crmTrait;
80+
use Wazza\SyncModelToCrm\Traits\HasCrmSync;
8181

8282
class User extends Authenticatable
8383
{
8484
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
8585

8686
// include this if you wish to use the `Mutators function` or
8787
// $this->syncToCrm() directly as appose to the observer method
88-
use crmTrait;
88+
use HasCrmSync;
8989

9090
/**
9191
* The attributes that are mass assignable.
@@ -277,115 +277,69 @@ class User extends Authenticatable
277277

278278
You can trigger a model sync in several ways:
279279

280-
1. **Directly in a Controller:**
280+
1. **Using the ShouldSyncToCrmOnSave (Automatic Sync on Save):**
281+
Add the `ShouldSyncToCrmOnSave` to your Eloquent model to automatically trigger a CRM sync every time the model is saved (created or updated). This is the easiest way to ensure your model stays in sync with your CRM provider without writing custom logic.
281282

282283
```php
283-
(new CrmController())->setModel($user)->execute();
284+
use Wazza\SyncModelToCrm\Traits\ShouldSyncToCrmOnSave;
285+
286+
class User extends Authenticatable
287+
{
288+
use ShouldSyncToCrmOnSave;
289+
// ...
290+
}
284291
```
285292

286-
2. **Using the Trait in a Mutator:**
287-
Call `$this->syncToCrm()` within your model.
293+
**When to use:**
294+
- Use `ShouldSyncToCrmOnSave` if you want your model to always sync to the CRM automatically after every save (create/update), with no extra code required.
295+
- This is ideal for most use cases where you want seamless, automatic syncing.
288296

289-
3. **Via an Observer:**
290-
Register an observer to automatically sync after save, update, delete, or restore events.
297+
2. **Using the HasCrmSync (Manual or Custom Sync):**
298+
Add the `HasCrmSync` to your model if you want to control exactly when the sync happens. This trait provides methods like `$this->syncToCrm()`, `$this->syncToCrmCreate()`, `$this->syncToCrmUpdate()`, etc., which you can call from mutators, custom methods, or anywhere in your application.
291299

292300
```php
293-
namespace App\Observers;
301+
use Wazza\SyncModelToCrm\Traits\HasCrmSync;
294302

295-
use App\Models\User;
296-
use Wazza\SyncModelToCrm\Http\Controllers\CrmController;
297-
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
303+
class User extends Authenticatable
304+
{
305+
use HasCrmSync;
306+
// ...
307+
}
298308

299-
class UserObserver implements ShouldHandleEventsAfterCommit
309+
public function save(array $options = [])
300310
{
301-
/**
302-
* Handle the User "created" event.
303-
*/
304-
public function created(User $user): void
305-
{
306-
echo ('create...');
307-
(new CrmController())
308-
->setModel($user)
309-
->setAttemptCreate()
310-
->execute(true);
311-
echo ('created...');
312-
}
313-
314-
/**
315-
* Handle the User "updated" event.
316-
*/
317-
public function updated(User $user): void
318-
{
319-
echo ('update...');
320-
(new CrmController())
321-
->setModel($user)
322-
->setAttemptUpdate()
323-
->execute(true);
324-
echo ('updated...');
325-
}
326-
327-
/**
328-
* Handle the User "deleted" event.
329-
* Run when a user is soft-deleted.
330-
*/
331-
public function deleted(User $user)
332-
{
333-
echo ('delete...');
334-
(new CrmController())
335-
->setModel($user)
336-
->setAttemptDelete()
337-
->execute();
338-
echo ('deleted...');
339-
}
340-
341-
/**
342-
* Handle the User "restored" event.
343-
* Soft-delete has been reversed.
344-
*/
345-
public function restored(User $user): void
346-
{
347-
echo ('restore...');
348-
(new CrmController())
349-
->setModel($user)
350-
->setAttemptRestore()
351-
->execute();
352-
echo ('restored...');
353-
}
354-
355-
/**
356-
* Handle the User "force deleted" event.
357-
*/
358-
public function forceDeleted(User $user): void
359-
{
360-
echo ('forceDeleted...');
361-
}
362-
363-
/**
364-
* Handle the User "saved" event.
365-
*
366-
*/
367-
public function saved(User $user): void
368-
{
369-
// echo ('saving...');
370-
// (new CrmController())
371-
// ->setModel($user)
372-
// ->setAttemptAll() // open for anything...
373-
// ->execute(true);
374-
// echo ('saved...');
375-
}
311+
parent::save($options);
312+
$this->syncToCrmPatch(); // Manually trigger sync after save
376313
}
377314
```
378315

379-
Register the observer in your `AppServiceProvider`:
316+
**When to use:**
317+
- Use `HasCrmSync` if you want to trigger syncs only at specific times, or if you need to customize the sync logic (e.g., only sync on certain conditions, or from a controller, observer, or job).
318+
- This is ideal for advanced use cases or when you want more granular control over syncing.
319+
320+
3. **Directly in a Controller:**
380321

381322
```php
382-
public function boot(): void
323+
// If CrmSyncController is registered as a singleton in the service container,
324+
// you should resolve it via dependency injection or the app() helper
325+
// to ensure you get the singleton instance:
326+
app(CrmSyncController::class)->setModel($user)->execute();
327+
328+
// Or, if you're inside a controller or method with dependency injection:
329+
public function syncUser(CrmSyncController $crmController, User $user)
383330
{
384-
\App\Models\User::observe(\App\Observers\UserObserver::class);
331+
$crmController->setModel($user)->execute();
385332
}
386333
```
387334

388-
4. **In a Job:**
335+
**Note:**
336+
Instantiating with `new CrmSyncController()` will always create a new instance, bypassing the singleton.
337+
To use the singleton, always resolve it from the container.
338+
339+
4. **Via an Observer:**
340+
Register an observer to automatically sync after save, update, delete, or restore events.
341+
342+
5. **In a Job:**
389343
Offload sync logic to a queued job for asynchronous processing.
390344

391345
---
@@ -441,7 +395,8 @@ You can trigger a model sync in several ways:
441395
```
442396

443397
6. **Review Configuration:**
444-
Adjust the published config file as needed.
398+
Adjust the published config file as needed. You also do not need all of the env settings above, have a look at the config file
399+
and overwrite any applicable items. Happy coding 😉
445400

446401
---
447402

@@ -458,7 +413,7 @@ tail -f storage/logs/laravel.log | grep sync-modeltocrm
458413

459414
## Testing
460415

461-
Run the test suite with:
416+
We have included a few unit tests, please expand if you wish to fork and help expand the functionalities.
462417

463418
```bash
464419
./vendor/bin/pest

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"package"
1111
],
1212
"homepage": "https://www.wazzac.dev",
13-
"repository": "https://github.com/wazzacdev/sync-model-to-crm",
13+
"repository": "https://github.com/wazzac/sync-model-to-crm",
1414
"readme": "README.md",
1515
"require": {
1616
"ext-json": "*",

0 commit comments

Comments
 (0)