Last active
November 16, 2018 12:30
-
-
Save Plytas/4ecbcf40a9af00e53358b23002b6cedf to your computer and use it in GitHub Desktop.
Trait for commands to run on tenants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Traits; | |
| use Hyn\Tenancy\Contracts\Repositories\WebsiteRepository; | |
| use Hyn\Tenancy\Environment; | |
| use Hyn\Tenancy\Repositories\HostnameRepository; | |
| use Illuminate\Database\Eloquent\Collection; | |
| trait RunsOnTenants | |
| { | |
| /** @var HostnameRepository */ | |
| private $hostnames; | |
| /** @var WebsiteRepository */ | |
| private $websites; | |
| /** @var Environment */ | |
| private $environment; | |
| public function __construct() | |
| { | |
| $this->websites = app(WebsiteRepository::class); | |
| $this->hostnames = app(HostnameRepository::class); | |
| $this->environment = app(Environment::class); | |
| parent::__construct(); | |
| } | |
| /** | |
| * Abstract function to force faker command to write logic in handleOnTenant() and leave handle to trait | |
| */ | |
| abstract function handleOnTenant(); | |
| public function handle() | |
| { | |
| if (! $this->confirmToProceed()) { | |
| return; | |
| } | |
| $headers = ['ID', 'Name', 'FQDN']; | |
| $items = [[0, 'All tenants', '']]; | |
| foreach ($this->websites->query()->get() as $website) | |
| { | |
| $items[] = [ | |
| $website->id, | |
| optional($website->customer)->name, | |
| $website->hostnames->first()->fqdn | |
| ]; | |
| } | |
| $this->table($headers, $items); | |
| $website_id = $this->ask('Which tenant to run this command on? (accepts comma separated ids'); | |
| $this->processHandle(function ($website) { | |
| $this->environment->tenant($website); | |
| $this->handleOnTenant(); | |
| }, $website_id); | |
| } | |
| protected function processHandle($callable, $website_id) | |
| { | |
| $query = $this->websites->query(); | |
| if ((int) $website_id != 0) { | |
| $query->whereIn('id', explode(',', $website_id)); | |
| } | |
| $query->orderBy('id')->chunk(10, function (Collection $websites) use ($callable) { | |
| $websites->each($callable); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment