From ea9ebf66d5592ae7ffd37d83d368550681483442 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Sun, 24 Jul 2022 21:17:06 -0500 Subject: [PATCH 01/12] Using popular default model paths for Laravel --- .gitignore | 1 + config/userlist.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5464500..7147568 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock /vendor/ /.php_cs.cache +.idea diff --git a/config/userlist.php b/config/userlist.php index 81e9c49..498c50e 100644 --- a/config/userlist.php +++ b/config/userlist.php @@ -1,8 +1,8 @@ \App\Model\User::class, - 'company_model' => \App\Model\Company::class, + 'user_model' => \App\Models\User::class, + 'company_model' => \App\Modesl\Team::class, 'push_id' => env('USERLIST_PUSH_ID'), 'push_key' => env('USERLIST_PUSH_KEY') ]; From 2554ab1522e897f3349e0bd01ecc504aa1e999cc Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Sun, 24 Jul 2022 21:17:47 -0500 Subject: [PATCH 02/12] a tad more OOP --- src/Console/Commands/ImportBaseCommand.php | 16 +++++++++++++++- src/Console/Commands/ImportCompaniesCommand.php | 4 ++-- src/Console/Commands/ImportUsersCommand.php | 4 ++-- src/Services/CompanyTransform.php | 5 +++++ src/Services/UserTransform.php | 5 +++++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Console/Commands/ImportBaseCommand.php b/src/Console/Commands/ImportBaseCommand.php index d0bb85f..9e479c5 100644 --- a/src/Console/Commands/ImportBaseCommand.php +++ b/src/Console/Commands/ImportBaseCommand.php @@ -5,7 +5,7 @@ use Illuminate\Console\Command; use Userlist\Laravel\Contracts\Push as Userlist; -class ImportBaseCommand extends Command +abstract class ImportBaseCommand extends Command { /** * The userlist client @@ -31,4 +31,18 @@ public function handle(Userlist $userlist) $this->import($entity); } } + + /** + * Import some data based on an Eloquent model + * @param $model + * @return null + */ + abstract protected function import($model); + + /** + * Return a full-qualified class name + * e.g. \App\Models\User + * @return string + */ + abstract protected function modelClass(); } diff --git a/src/Console/Commands/ImportCompaniesCommand.php b/src/Console/Commands/ImportCompaniesCommand.php index cc685c5..a9756da 100644 --- a/src/Console/Commands/ImportCompaniesCommand.php +++ b/src/Console/Commands/ImportCompaniesCommand.php @@ -18,8 +18,8 @@ class ImportCompaniesCommand extends ImportBaseCommand */ protected $description = 'Imports all existing companies into Userlist'; - protected function import($company) { - $this->userlist->company($company); + protected function import($model) { + $this->userlist->company($model); } protected function modelClass() { diff --git a/src/Console/Commands/ImportUsersCommand.php b/src/Console/Commands/ImportUsersCommand.php index abccfc9..fd7bc49 100644 --- a/src/Console/Commands/ImportUsersCommand.php +++ b/src/Console/Commands/ImportUsersCommand.php @@ -18,8 +18,8 @@ class ImportUsersCommand extends ImportBaseCommand */ protected $description = 'Imports all existing users into Userlist'; - protected function import($user) { - $this->userlist->user($user); + protected function import($model) { + $this->userlist->user($model); } protected function modelClass() { diff --git a/src/Services/CompanyTransform.php b/src/Services/CompanyTransform.php index 4db79e8..fe6c14c 100644 --- a/src/Services/CompanyTransform.php +++ b/src/Services/CompanyTransform.php @@ -6,6 +6,11 @@ use Illuminate\Support\Str; class CompanyTransform implements Transform { + + /** + * @param $company + * @return array + */ public function transform($company) { $modelName = Str::slug((class_basename(get_class($company)))); diff --git a/src/Services/UserTransform.php b/src/Services/UserTransform.php index 31306e4..a64ee83 100644 --- a/src/Services/UserTransform.php +++ b/src/Services/UserTransform.php @@ -6,6 +6,11 @@ use Illuminate\Support\Str; class UserTransform implements Transform { + + /** + * @param $user + * @return array + */ public function transform($user) { $modelName = Str::slug((class_basename(get_class($user)))); From c28028af9fcb5d157940c5c280f8991135d43d87 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Sun, 24 Jul 2022 21:18:22 -0500 Subject: [PATCH 03/12] a trait to add to models for importing --- src/ImportsToUserlist.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/ImportsToUserlist.php diff --git a/src/ImportsToUserlist.php b/src/ImportsToUserlist.php new file mode 100644 index 0000000..c4d14b6 --- /dev/null +++ b/src/ImportsToUserlist.php @@ -0,0 +1,24 @@ +toUserlist(); + } + + if (__CLASS__ == config('userlist.user_model')) { + return (new UserTransform())->transform($this); + } + } +} \ No newline at end of file From d26563b8447ea6503a1534f0fc53e9a59626eb59 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Sun, 24 Jul 2022 21:18:27 -0500 Subject: [PATCH 04/12] wip --- src/Services/EventTransform.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Services/EventTransform.php b/src/Services/EventTransform.php index 37c40df..b195d65 100644 --- a/src/Services/EventTransform.php +++ b/src/Services/EventTransform.php @@ -14,6 +14,7 @@ public function __construct(UserTransform $userTransform, CompanyTransform $comp } public function transform($event) { + $event['user'] = $this->userTransform->transform($event['user']); $event['company'] = $this->companyTransform->transform($event['company']); From c97e9c7cb191184a0cc554be5e069140615af06d Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Sun, 24 Jul 2022 21:35:56 -0500 Subject: [PATCH 05/12] typo fix, finished implementing trait for user and company import --- config/userlist.php | 2 +- src/ImportsToUserlist.php | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/config/userlist.php b/config/userlist.php index 498c50e..9716a3a 100644 --- a/config/userlist.php +++ b/config/userlist.php @@ -2,7 +2,7 @@ return [ 'user_model' => \App\Models\User::class, - 'company_model' => \App\Modesl\Team::class, + 'company_model' => \App\Models\Team::class, 'push_id' => env('USERLIST_PUSH_ID'), 'push_key' => env('USERLIST_PUSH_KEY') ]; diff --git a/src/ImportsToUserlist.php b/src/ImportsToUserlist.php index c4d14b6..2ffd738 100644 --- a/src/ImportsToUserlist.php +++ b/src/ImportsToUserlist.php @@ -3,12 +3,13 @@ namespace Userlist\Laravel; +use Userlist\Laravel\Services\CompanyTransform; use Userlist\Laravel\Services\UserTransform; trait ImportsToUserlist { /** - * T + * Transform a model to a Userlist object * @return array|void */ public function transformForUserlist() @@ -18,7 +19,11 @@ public function transformForUserlist() } if (__CLASS__ == config('userlist.user_model')) { - return (new UserTransform())->transform($this); + return (new UserTransform)->transform($this); + } + + if (__CLASS__ == config('userlist.company_model')) { + return (new CompanyTransform)->transform($this); } } } \ No newline at end of file From be2bcdb3efae00060cdd20c0b3b2410bed40bed9 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Sun, 24 Jul 2022 21:44:35 -0500 Subject: [PATCH 06/12] updated for new transformation style. TODO: Event transformations --- src/ImportsToUserlist.php | 3 +++ src/Services/DirectPush.php | 15 ++++----------- src/Services/EventTransform.php | 11 ++--------- src/Services/NullPush.php | 12 +++--------- 4 files changed, 12 insertions(+), 29 deletions(-) diff --git a/src/ImportsToUserlist.php b/src/ImportsToUserlist.php index 2ffd738..c662d98 100644 --- a/src/ImportsToUserlist.php +++ b/src/ImportsToUserlist.php @@ -25,5 +25,8 @@ public function transformForUserlist() if (__CLASS__ == config('userlist.company_model')) { return (new CompanyTransform)->transform($this); } + + // todo: Auto Event transform based on class namespace, + // or existence of $this['user'] / $this['company'] } } \ No newline at end of file diff --git a/src/Services/DirectPush.php b/src/Services/DirectPush.php index f399c6f..6144e9f 100644 --- a/src/Services/DirectPush.php +++ b/src/Services/DirectPush.php @@ -3,22 +3,15 @@ namespace Userlist\Laravel\Services; use Userlist\Laravel\Contracts\Push; -use Userlist\Laravel\Contracts\UserTransform; -use Userlist\Laravel\Contracts\CompanyTransform; -use Userlist\Laravel\Contracts\EventTransform; class DirectPush implements Push { - public function __construct(\Userlist\Push $push, UserTransform $userTransform, CompanyTransform $companyTransform, EventTransform $eventTransform) + public function __construct(\Userlist\Push $push) { $this->push = $push; - - $this->userTransform = $userTransform; - $this->companyTransform = $companyTransform; - $this->eventTransform = $eventTransform; } public function user($user) { - $payload = $this->userTransform->transform($user); + $payload = $user->transformForUserlist(); if ($payload != null) { try { @@ -30,7 +23,7 @@ public function user($user) { } public function company($company) { - $payload = $this->companyTransform->transform($company); + $payload = $company->transformForUserlist(); if ($payload != null) { try { @@ -41,7 +34,7 @@ public function company($company) { } } - public function event($event) { + public function event($event){ $payload = $this->eventTransform->transform($event); if ($payload != null) { diff --git a/src/Services/EventTransform.php b/src/Services/EventTransform.php index b195d65..228d151 100644 --- a/src/Services/EventTransform.php +++ b/src/Services/EventTransform.php @@ -3,20 +3,13 @@ namespace Userlist\Laravel\Services; use Userlist\Laravel\Contracts\EventTransform as Transform; -use Userlist\Laravel\Contracts\UserTransform; -use Userlist\Laravel\Contracts\CompanyTransform; class EventTransform implements Transform { - public function __construct(UserTransform $userTransform, CompanyTransform $companyTransform) - { - $this->userTransform = $userTransform; - $this->companyTransform = $companyTransform; - } public function transform($event) { - $event['user'] = $this->userTransform->transform($event['user']); - $event['company'] = $this->companyTransform->transform($event['company']); + $event['user'] = $event['user']->transformForUserlist(); + $event['company'] = $event['company']->transformForUserlist(); return $event; } diff --git a/src/Services/NullPush.php b/src/Services/NullPush.php index 36c3e97..46624c0 100644 --- a/src/Services/NullPush.php +++ b/src/Services/NullPush.php @@ -8,22 +8,16 @@ use Userlist\Laravel\Contracts\EventTransform; class NullPush implements Push { - public function __construct(UserTransform $userTransform, CompanyTransform $companyTransform, EventTransform $eventTransform) - { - $this->userTransform = $userTransform; - $this->companyTransform = $companyTransform; - $this->eventTransform = $eventTransform; - } public function user($user) { - $this->userTransform->transform($user); + $user->transformForUserlist(); } public function company($company) { - $this->companyTransform->transform($company); + $company->transformForUserlist(); } public function event($event) { - $this->eventTransform->transform($event); + $event->transformForUserlist(); } } From 2c926b1175fccea85a5900b89b4af61ca7a0e099 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Sun, 24 Jul 2022 21:49:28 -0500 Subject: [PATCH 07/12] fallback to event transform if its not a user nor company transform --- src/ImportsToUserlist.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/ImportsToUserlist.php b/src/ImportsToUserlist.php index c662d98..bef782d 100644 --- a/src/ImportsToUserlist.php +++ b/src/ImportsToUserlist.php @@ -4,7 +4,9 @@ use Userlist\Laravel\Services\CompanyTransform; +use Userlist\Laravel\Services\EventTransform; use Userlist\Laravel\Services\UserTransform; +use Userlist\Push; trait ImportsToUserlist { @@ -20,13 +22,10 @@ public function transformForUserlist() if (__CLASS__ == config('userlist.user_model')) { return (new UserTransform)->transform($this); - } - - if (__CLASS__ == config('userlist.company_model')) { + } else if (__CLASS__ == config('userlist.company_model')) { return (new CompanyTransform)->transform($this); + } else { + return (new EventTransform)->transform($this); } - - // todo: Auto Event transform based on class namespace, - // or existence of $this['user'] / $this['company'] } } \ No newline at end of file From 9495c44614ee7928d853cd929c7baf9476898765 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Sun, 24 Jul 2022 22:08:14 -0500 Subject: [PATCH 08/12] a start at readme updates --- README.md | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/README.md b/README.md index f33f665..ae53c87 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,82 @@ composer require userlist/userlist-laravel ## Configuration +There are 2 configuration steps. + +**First, publish the configuration file:** + +After installation, you can publish the configuration file for Userlist: + +```bash +php artisan vendor:publish --provider Userlist\Laravel\UserlistServiceProvider +``` + +You can then edit the `config/userlist.php` file if needed: + +```php +# Adjust if your User/Team models are found elsewhere +return [ + 'user_model' => \App\Models\User::class, + 'company_model' => \App\Models\Team::class, + 'push_id' => env('USERLIST_PUSH_ID'), + 'push_key' => env('USERLIST_PUSH_KEY') +]; +``` + +**Second, add the Userlist trait to your models:** + +```php +# Other items omitted +use Userlist\Laravel\ImportsToUserlist; + +class User extends Authenticatable +{ + # Other items omitted + use ImportsToUserlist; + + ... +} +``` + +User and Team (Company) models need to be transformed to data Userlist can understand (https://userlist.com/docs/getting-started/integration-guide/). + +If you need to customize how Userlist transforms your models data, you can add a `toUserlist()` method to your classes: + +```php +# Other items omitted +use Userlist\Laravel\ImportsToUserlist; + +class User extends Authenticatable +{ + # Other items omitted + use ImportsToUserlist; + + public function toUserlist() + { + $modelName = Str::slug((class_basename(get_class($user)))); + + return [ + 'identifier' => "user-$this->getKey()", + 'email' => $user->email, + 'signed_up_at' => $user->created_at, + 'properties' => [ + 'name' => $user->name + ], + 'companies' => $this->allTeams()->map(function(Team $team) { + // Assuming the team also uses uses `ImportsToUserlist` + return $team->transformForUserlist(); + })->toArray(); + ]; + } +} +``` ## Usage +### Importing Current Data + +### Tracking Companies + ### Tracking Users From 081c06526d10c8494dc84703d1bf65c9dc5b6b09 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Mon, 25 Jul 2022 18:21:24 -0500 Subject: [PATCH 09/12] Revert "updated for new transformation style. TODO: Event transformations" This reverts commit be2bcdb3efae00060cdd20c0b3b2410bed40bed9. --- src/Services/DirectPush.php | 15 +++++++++++---- src/Services/EventTransform.php | 11 +++++++++-- src/Services/NullPush.php | 12 +++++++++--- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Services/DirectPush.php b/src/Services/DirectPush.php index 6144e9f..f399c6f 100644 --- a/src/Services/DirectPush.php +++ b/src/Services/DirectPush.php @@ -3,15 +3,22 @@ namespace Userlist\Laravel\Services; use Userlist\Laravel\Contracts\Push; +use Userlist\Laravel\Contracts\UserTransform; +use Userlist\Laravel\Contracts\CompanyTransform; +use Userlist\Laravel\Contracts\EventTransform; class DirectPush implements Push { - public function __construct(\Userlist\Push $push) + public function __construct(\Userlist\Push $push, UserTransform $userTransform, CompanyTransform $companyTransform, EventTransform $eventTransform) { $this->push = $push; + + $this->userTransform = $userTransform; + $this->companyTransform = $companyTransform; + $this->eventTransform = $eventTransform; } public function user($user) { - $payload = $user->transformForUserlist(); + $payload = $this->userTransform->transform($user); if ($payload != null) { try { @@ -23,7 +30,7 @@ public function user($user) { } public function company($company) { - $payload = $company->transformForUserlist(); + $payload = $this->companyTransform->transform($company); if ($payload != null) { try { @@ -34,7 +41,7 @@ public function company($company) { } } - public function event($event){ + public function event($event) { $payload = $this->eventTransform->transform($event); if ($payload != null) { diff --git a/src/Services/EventTransform.php b/src/Services/EventTransform.php index 228d151..b195d65 100644 --- a/src/Services/EventTransform.php +++ b/src/Services/EventTransform.php @@ -3,13 +3,20 @@ namespace Userlist\Laravel\Services; use Userlist\Laravel\Contracts\EventTransform as Transform; +use Userlist\Laravel\Contracts\UserTransform; +use Userlist\Laravel\Contracts\CompanyTransform; class EventTransform implements Transform { + public function __construct(UserTransform $userTransform, CompanyTransform $companyTransform) + { + $this->userTransform = $userTransform; + $this->companyTransform = $companyTransform; + } public function transform($event) { - $event['user'] = $event['user']->transformForUserlist(); - $event['company'] = $event['company']->transformForUserlist(); + $event['user'] = $this->userTransform->transform($event['user']); + $event['company'] = $this->companyTransform->transform($event['company']); return $event; } diff --git a/src/Services/NullPush.php b/src/Services/NullPush.php index 46624c0..36c3e97 100644 --- a/src/Services/NullPush.php +++ b/src/Services/NullPush.php @@ -8,16 +8,22 @@ use Userlist\Laravel\Contracts\EventTransform; class NullPush implements Push { + public function __construct(UserTransform $userTransform, CompanyTransform $companyTransform, EventTransform $eventTransform) + { + $this->userTransform = $userTransform; + $this->companyTransform = $companyTransform; + $this->eventTransform = $eventTransform; + } public function user($user) { - $user->transformForUserlist(); + $this->userTransform->transform($user); } public function company($company) { - $company->transformForUserlist(); + $this->companyTransform->transform($company); } public function event($event) { - $event->transformForUserlist(); + $this->eventTransform->transform($event); } } From 2e6ca4189c701978891693b685ff6068384a1b46 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Mon, 25 Jul 2022 18:24:19 -0500 Subject: [PATCH 10/12] instead of a trait, check if `toUserlist()` method exists on the model/event object --- src/ImportsToUserlist.php | 31 ------------------------------- src/Services/CompanyTransform.php | 4 ++++ src/Services/EventTransform.php | 3 +++ src/Services/UserTransform.php | 4 ++++ 4 files changed, 11 insertions(+), 31 deletions(-) delete mode 100644 src/ImportsToUserlist.php diff --git a/src/ImportsToUserlist.php b/src/ImportsToUserlist.php deleted file mode 100644 index bef782d..0000000 --- a/src/ImportsToUserlist.php +++ /dev/null @@ -1,31 +0,0 @@ -toUserlist(); - } - - if (__CLASS__ == config('userlist.user_model')) { - return (new UserTransform)->transform($this); - } else if (__CLASS__ == config('userlist.company_model')) { - return (new CompanyTransform)->transform($this); - } else { - return (new EventTransform)->transform($this); - } - } -} \ No newline at end of file diff --git a/src/Services/CompanyTransform.php b/src/Services/CompanyTransform.php index fe6c14c..72af6a4 100644 --- a/src/Services/CompanyTransform.php +++ b/src/Services/CompanyTransform.php @@ -12,6 +12,10 @@ class CompanyTransform implements Transform { * @return array */ public function transform($company) { + if (method_exists($company, 'toUserlist')) { + return $company->toUserlist(); + } + $modelName = Str::slug((class_basename(get_class($company)))); return [ diff --git a/src/Services/EventTransform.php b/src/Services/EventTransform.php index b195d65..4c106f5 100644 --- a/src/Services/EventTransform.php +++ b/src/Services/EventTransform.php @@ -14,6 +14,9 @@ public function __construct(UserTransform $userTransform, CompanyTransform $comp } public function transform($event) { + if (method_exists($event, 'toUserlist')) { + return $event->toUserlist(); + } $event['user'] = $this->userTransform->transform($event['user']); $event['company'] = $this->companyTransform->transform($event['company']); diff --git a/src/Services/UserTransform.php b/src/Services/UserTransform.php index a64ee83..0596253 100644 --- a/src/Services/UserTransform.php +++ b/src/Services/UserTransform.php @@ -12,6 +12,10 @@ class UserTransform implements Transform { * @return array */ public function transform($user) { + if (method_exists($user, 'toUserlist')) { + return $user->toUserlist(); + } + $modelName = Str::slug((class_basename(get_class($user)))); return [ From 92f3db02e75ece1d633e7ffed13d948d4dd498d1 Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Mon, 25 Jul 2022 19:01:13 -0500 Subject: [PATCH 11/12] readme updates --- README.md | 91 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index ae53c87..369837d 100644 --- a/README.md +++ b/README.md @@ -12,17 +12,13 @@ composer require userlist/userlist-laravel ## Configuration -There are 2 configuration steps. - -**First, publish the configuration file:** - After installation, you can publish the configuration file for Userlist: ```bash php artisan vendor:publish --provider Userlist\Laravel\UserlistServiceProvider ``` -You can then edit the `config/userlist.php` file if needed: +You can then edit the `config/userlist.php` file if needed. ```php # Adjust if your User/Team models are found elsewhere @@ -30,42 +26,19 @@ return [ 'user_model' => \App\Models\User::class, 'company_model' => \App\Models\Team::class, 'push_id' => env('USERLIST_PUSH_ID'), - 'push_key' => env('USERLIST_PUSH_KEY') + 'push_key' => env('USERLIST_PUSH_KEY'), ]; ``` -**Second, add the Userlist trait to your models:** - -```php -# Other items omitted -use Userlist\Laravel\ImportsToUserlist; - -class User extends Authenticatable -{ - # Other items omitted - use ImportsToUserlist; - - ... -} -``` - -User and Team (Company) models need to be transformed to data Userlist can understand (https://userlist.com/docs/getting-started/integration-guide/). +Userlist will attempt to transform your `User` and `Team` (Company) models to data it can understand automatically (an associated array that can be encoded as JSON).. -If you need to customize how Userlist transforms your models data, you can add a `toUserlist()` method to your classes: +However, if you need or want to customize the User/Team (Company)/Event objects sent to Userlist, you can add a public `toUserList()` method to your Model or Event classes: ```php -# Other items omitted -use Userlist\Laravel\ImportsToUserlist; - class User extends Authenticatable -{ - # Other items omitted - use ImportsToUserlist; - +{ public function toUserlist() { - $modelName = Str::slug((class_basename(get_class($user)))); - return [ 'identifier' => "user-$this->getKey()", 'email' => $user->email, @@ -73,9 +46,10 @@ class User extends Authenticatable 'properties' => [ 'name' => $user->name ], + // Assuming allTeams() is a method of this class 'companies' => $this->allTeams()->map(function(Team $team) { - // Assuming the team also uses uses `ImportsToUserlist` - return $team->transformForUserlist(); + // Assuming the team also has a `toUserList()` method + return $team->toUserList(); })->toArray(); ]; } @@ -86,15 +60,64 @@ class User extends Authenticatable ### Importing Current Data +This package includes some commands to help you load your current data (users and companies) into Userlist. After using these commands, you should also implement code +to register users and companies to Userlist as they are created or updated. + +The `userlist:import` command imports all users and companies into Userlist. These uses the configuration for `user_model` +and `company_model` to decide which Eloquent models to include. + +```bash +# This command calls the following under the hood +# userlist:import:companies +# userlist:import:users +php artisan userlist:import +``` + +The `userlist:import:companies` command will use the Eloquent model defined by the `company_model` configuration to import all companies. + +In Laravel, this is often the `App\Models\Team` model. + +```bash +php artisan userlist:import:companies +``` + +The `userlist:import:users` command will use the Eloquent model defined by the `user_model` configuration to import all users. + +In Laravel, this is often the `App\Models\User` model. + +```bash +php artisan userlist:import:users +``` + ### Tracking Companies +You can track companies (adding and updating) by "pushing" their records to Userlist. + + +```php +// In Laravel, Userlist companies are often "teams" +$team = App\Models\Team::find(1); +app(Userlist\Laravel\Contracts\Push::class)->company($team); +``` + +These will transform the model to a "jsonable" associated array using the model's `toUserList()` method (if defined), else fall back to its default transformation logic for the class. ### Tracking Users +You can track users (adding and updating) by "pushing" their records to Userlist. + + +```php +$user = App\Models\User::find(1); +app(Userlist\Laravel\Contracts\Push::class)->user($user); +``` + +These will transform the model to a "jsonable" associated array using the model's `toUserList()` method (if defined), else fall back to its default transformation logic for the class. ### Tracking Events + ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/userlist/userlist-laravel. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. From 6c7e2f2b26a7b040bf13c23829f52e76c78879dc Mon Sep 17 00:00:00 2001 From: Chris Fidao Date: Mon, 25 Jul 2022 20:50:04 -0500 Subject: [PATCH 12/12] A stab at automatically handling event pushing, also renamed some variables to match their interface --- src/Services/CompanyTransform.php | 16 ++++++++-------- src/Services/EventTransform.php | 31 +++++++++++++++++++++++++------ src/Services/UserTransform.php | 18 +++++++++--------- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/Services/CompanyTransform.php b/src/Services/CompanyTransform.php index 72af6a4..5210a5f 100644 --- a/src/Services/CompanyTransform.php +++ b/src/Services/CompanyTransform.php @@ -8,20 +8,20 @@ class CompanyTransform implements Transform { /** - * @param $company + * @param $entity * @return array */ - public function transform($company) { - if (method_exists($company, 'toUserlist')) { - return $company->toUserlist(); + public function transform($entity) { + if (method_exists($entity, 'toUserlist')) { + return $entity->toUserlist(); } - $modelName = Str::slug((class_basename(get_class($company)))); + $modelName = Str::slug((class_basename(get_class($entity)))); return [ - 'identifier' => "$modelName-$company->id", - 'name' => $company->name, - 'signed_up_at' => $company->created_at, + 'identifier' => "$modelName-$entity->id", + 'name' => $entity->name, + 'signed_up_at' => $entity->created_at, ]; } } diff --git a/src/Services/EventTransform.php b/src/Services/EventTransform.php index 4c106f5..5fdd7c7 100644 --- a/src/Services/EventTransform.php +++ b/src/Services/EventTransform.php @@ -2,6 +2,7 @@ namespace Userlist\Laravel\Services; +use Illuminate\Support\Str; use Userlist\Laravel\Contracts\EventTransform as Transform; use Userlist\Laravel\Contracts\UserTransform; use Userlist\Laravel\Contracts\CompanyTransform; @@ -13,14 +14,32 @@ public function __construct(UserTransform $userTransform, CompanyTransform $comp $this->companyTransform = $companyTransform; } - public function transform($event) { - if (method_exists($event, 'toUserlist')) { - return $event->toUserlist(); + public function transform($entity) { + if (method_exists($entity, 'toUserlist')) { + return $entity->toUserlist(); } - $event['user'] = $this->userTransform->transform($event['user']); - $event['company'] = $this->companyTransform->transform($event['company']); + if ($entity instanceof \Illuminate\Contracts\Support\Arrayable) { + return $entity->toArray(); + } + + // If the event doesn't have a way to give us data, we'll attempt to build it + // from the available information. + // The event ideally has a public property "user", "team", or "company" + $data = []; + + // An event should only have a user OR a company identifier + if (property_exists($entity, 'user')) { + $data['user'] = $this->userTransform->transform($entity->user)['identifier']; + } elseif (property_exists($entity, 'company')) { + $data['company'] = $this->companyTransform->transform($entity->company)['identifier']; + } elseif (property_exists($entity, 'team')) { + $data['company'] = $this->companyTransform->transform($entity->team)['identifier']; + } + + $modelName = Str::slug((class_basename(get_class($entity)))); + $data['name'] = "event-$modelName"; - return $event; + return $data; } } diff --git a/src/Services/UserTransform.php b/src/Services/UserTransform.php index 0596253..f598dea 100644 --- a/src/Services/UserTransform.php +++ b/src/Services/UserTransform.php @@ -8,22 +8,22 @@ class UserTransform implements Transform { /** - * @param $user + * @param $entity * @return array */ - public function transform($user) { - if (method_exists($user, 'toUserlist')) { - return $user->toUserlist(); + public function transform($entity) { + if (method_exists($entity, 'toUserlist')) { + return $entity->toUserlist(); } - $modelName = Str::slug((class_basename(get_class($user)))); + $modelName = Str::slug((class_basename(get_class($entity)))); return [ - 'identifier' => "$modelName-$user->id", - 'email' => $user->email, - 'signed_up_at' => $user->created_at, + 'identifier' => "$modelName-$entity->id", + 'email' => $entity->email, + 'signed_up_at' => $entity->created_at, 'properties' => [ - 'name' => $user->name + 'name' => $entity->name ] ]; }