Skip to content

Commit

Permalink
Merge branch 'master' into windows-directories
Browse files Browse the repository at this point in the history
  • Loading branch information
TeslaDethray authored Jan 21, 2017
2 parents 9382603 + a289172 commit 092a681
Show file tree
Hide file tree
Showing 22 changed files with 1,395 additions and 580 deletions.
37 changes: 18 additions & 19 deletions src/Collections/SiteOrganizationMemberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ class SiteOrganizationMemberships extends SiteOwnedCollection
**/
public function create($name, $role)
{
$workflow = $this->getSite()->getWorkflows()->create(
return $this->getSite()->getWorkflows()->create(
'add_site_organization_membership',
['params' => ['organization_name' => $name, 'role' => $role,],]
);
return $workflow;
}

/**
Expand All @@ -59,23 +58,6 @@ public function findByName($name)
return null;
}

/**
* Returns UUID of organization with given name
*
* @param string $name A name to search for
* @return SiteOrganizationMembership|null
*/
public function getUUID($name)
{
foreach ($this->models as $org_member) {
$org = $org_member->getName();
if ($name == $org) {
return $org_member;
}
}
return null;
}

/**
* Retrieves the model with organization of the given UUID or name
*
Expand All @@ -99,4 +81,21 @@ public function get($id)
['org' => $id, 'site' => $this->site->getName(),]
);
}

/**
* Returns UUID of organization with given name
*
* @param string $name A name to search for
* @return SiteOrganizationMembership|null
*/
public function getUUID($name)
{
foreach ($this->models as $org_member) {
$org = $org_member->getName();
if ($name == $org) {
return $org_member;
}
}
return null;
}
}
180 changes: 64 additions & 116 deletions src/Collections/Workflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Pantheon\Terminus\Collections;

use Pantheon\Terminus\Exceptions\TerminusException;
use Pantheon\Terminus\Models\Environment;
use Pantheon\Terminus\Models\Organization;
use Pantheon\Terminus\Models\Site;
use Pantheon\Terminus\Models\TerminusModel;
use Pantheon\Terminus\Models\User;
use Pantheon\Terminus\Models\Workflow;
use Pantheon\Terminus\Session\SessionAwareInterface;
use Pantheon\Terminus\Session\SessionAwareTrait;
Expand All @@ -14,30 +20,14 @@ class Workflows extends TerminusCollection implements SessionAwareInterface
{
use SessionAwareTrait;

/**
* @var mixed
*/
protected $owner;
/**
* @var Environment
*/
private $environment;
/**
* @var Organization
*/
private $organization;
/**
* @var Site
*/
private $site;
/**
* @var User
*/
private $user;
/**
* @var string
*/
protected $collected_class = Workflow::class;
/**
* @var TerminusModel
*/
protected $owner;

/**
* Instantiates the collection, sets param members as properties
Expand All @@ -48,59 +38,14 @@ public function __construct(array $options = [])
{
parent::__construct($options);
if (isset($options['environment'])) {
$this->owner = $this->environment = $options['environment'];
$this->owner = $options['environment'];
} elseif (isset($options['organization'])) {
$this->owner = $this->organization = $options['organization'];
$this->owner = $options['organization'];
} elseif (isset($options['site'])) {
$this->owner = $this->site = $options['site'];
$this->owner = $options['site'];
} elseif (isset($options['user'])) {
$this->owner = $this->user = $options['user'];
}
}

/**
* Get the URL for this model
*
* @return string
*/
public function getUrl()
{
if (!empty($this->url)) {
return $this->url;
}

// Determine the url based on the workflow owner.
$owner = $this->getOwnerObject();
switch (get_class($owner)) {
case 'Pantheon\Terminus\Models\Environment':
$this->url = sprintf(
'sites/%s/environments/%s/workflows',
$owner->site->id,
$owner->id
);
break;
case 'Pantheon\Terminus\Models\Organization':
$this->url = sprintf(
'users/%s/organizations/%s/workflows',
// @TODO: This should be passed in rather than read from the current session.
$this->session()->getUser()->id,
$owner->id
);
break;
case 'Pantheon\Terminus\Models\Site':
$this->url = sprintf(
'sites/%s/workflows',
$owner->id
);
break;
case 'Pantheon\Terminus\Models\User':
$this->url = sprintf(
'users/%s/workflows',
$owner->id
);
break;
$this->owner = $options['user'];
}
return $this->url;
}

/**
Expand All @@ -110,14 +55,12 @@ public function getUrl()
*/
public function allFinished()
{
$workflows = array_filter(
return array_filter(
$this->all(),
function ($workflow) {
$is_finished = $workflow->isFinished();
return $is_finished;
return $workflow->isFinished();
}
);
return $workflows;
}

/**
Expand All @@ -127,16 +70,12 @@ function ($workflow) {
*/
public function allWithLogs()
{
$workflows = $this->allFinished();
$workflows = array_filter(
$workflows,
return array_filter(
$this->allFinished(),
function ($workflow) {
$has_logs = $workflow->get('has_operation_log_output');
return $has_logs;
return $workflow->get('has_operation_log_output');
}
);

return $workflows;
}

/**
Expand Down Expand Up @@ -179,13 +118,44 @@ public function create($type, array $options = [])
/**
* Returns the object which controls this collection
*
* @return mixed
* @return TerminusModel
*/
public function getOwnerObject()
{
return $this->owner;
}

/**
* Get the URL for this model
*
* @return string
*/
public function getUrl()
{
if (!empty($this->url)) {
return $this->url;
}

// Determine the url based on the workflow owner.
$owner = $this->getOwnerObject();
switch (get_class($owner)) {
case Environment::class:
$this->url = "sites/{$owner->site->id}/environments/{$owner->id}/workflows";
break;
case Organization::class:
$this->url = "users/{$this->session()->getUser()->id}/organizations/{$owner->id}/workflows";
// @TODO: This should be passed in rather than read from the current session.
break;
case Site::class:
$this->url = "sites/{$owner->id}/workflows";
break;
case User::class:
$this->url = "users/{$owner->id}/workflows";
break;
}
return $this->url;
}

/**
* Fetches workflow data hydrated with operations
*
Expand All @@ -212,22 +182,14 @@ public function findLatestWithLogs()
usort(
$workflows,
function ($a, $b) {
$a_finished_after_b = $a->get('finished_at') >= $b->get('finished_at');
if ($a_finished_after_b) {
$cmp = -1;
} else {
$cmp = 1;
}
return $cmp;
return ($a->wasFinishedAfter($b->get('finished_at'))) ? -1 : 1;
}
);

if (count($workflows) > 0) {
$workflow = $workflows[0];
} else {
$workflow = null;
return $workflows[0];
}
return $workflow;
return null;
}

/**
Expand All @@ -241,21 +203,14 @@ public function lastCreatedAt()
usort(
$workflows,
function ($a, $b) {
$a_created_after_b = $a->get('created_at') >= $b->get('created_at');
if ($a_created_after_b) {
$cmp = -1;
} else {
$cmp = 1;
}
return $cmp;
return ($a->wasCreatedAfter($b->get('created_at'))) ? -1 : 1;
}
);
if (count($workflows) > 0) {
$timestamp = $workflows[0]->get('created_at');
} else {
$timestamp = null;
if (!empty($workflows)) {
$workflow = array_shift($workflows);
return $workflow->get('created_at');
}
return $timestamp;
return null;
}

/**
Expand All @@ -269,20 +224,13 @@ public function lastFinishedAt()
usort(
$workflows,
function ($a, $b) {
$a_finished_after_b = $a->get('finished_at') >= $b->get('finished_at');
if ($a_finished_after_b) {
$cmp = -1;
} else {
$cmp = 1;
}
return $cmp;
return ($a->wasFinishedAfter($b->get('finished_at'))) ? -1 : 1;
}
);
if (count($workflows) > 0) {
$timestamp = $workflows[0]->get('finished_at');
} else {
$timestamp = null;
if (!empty($workflows)) {
$workflow = array_shift($workflows);
return $workflow->get('finished_at');
}
return $timestamp;
return null;
}
}
Loading

0 comments on commit 092a681

Please sign in to comment.