Skip to content

Commit

Permalink
Add a Model/Collection for Bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Jul 29, 2015
1 parent 844a2b9 commit 6c3b766
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
39 changes: 39 additions & 0 deletions php/Terminus/Collections/Bindings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Terminus\Collections;
use Terminus\Request;
use Terminus\Models\Binding;
use \Terminus_Command;

class Bindings {
private $environment;
private $models = array();

public function __construct($options = array()) {
$this->environment = $options['environment'];

return $this;
}

public function fetch() {
$results = Terminus_Command::request("sites", $this->environment->site->getId(), "bindings", "GET");

foreach (get_object_vars($results['data']) as $id => $binding_data) {
# Only include bindings for this environment
if ($binding_data->environment == $this->environment->id) {
$binding_data->id = $id;
$this->models[$id] = new Binding($binding_data, array('collection' => $this));
}
}

return $this;
}

public function get($id) {
return array_key_exists($id, $this->models) ? $this->models[$id] : null;
}

public function all() {
return array_values($this->models);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Terminus;
namespace Terminus\Collections;
use Terminus\Request;
use Terminus\Environment;
use \Terminus_Command;
Expand All @@ -23,7 +23,7 @@ public function fetch() {
$this->models[$id] = new Environment($this->site, $environment_data);
}

return $this->all();
return $this;
}

public function get($id) {
Expand Down
9 changes: 7 additions & 2 deletions php/Terminus/Environment.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php
namespace Terminus;
use \ReflectionClass, \Terminus\Request, \Terminus\EnvironmentWorkflow;
use \ReflectionClass;
use \Terminus\Request;
use \Terminus\EnvironmentWorkflow;
use \Terminus\Collections\Bindings;

class Environment {
public $id;
public $attributes;
public $bindings;

public $name = 'dev';
public $site = false;
Expand All @@ -19,7 +23,6 @@ class Environment {
public $target_ref;
public $watchers;
public $backups;
public $bindings;

public function __construct( Site $site, $data = null) {
$this->site = $site;
Expand All @@ -28,6 +31,8 @@ public function __construct( Site $site, $data = null) {
}
$this->attributes = $data;

$this->bindings = new Bindings(array('environment' => $this));

if (is_object($data)) {
// if we receive an environment object from the api hydrate the vars
$environment_properties = get_object_vars($data);
Expand Down
19 changes: 19 additions & 0 deletions php/Terminus/Models/Binding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Terminus\Models;
use \Terminus\Request;

class Binding {
public $id;
public $attributes;
public $collection;
public $environment;

public function __construct($attributes, $options = array()) {
$this->id = $attributes->id;
$this->attributes = $attributes;
$this->collection = $options['collection'];
$this->environment = $options['environment'];

return $this;
}
}
2 changes: 1 addition & 1 deletion php/Terminus/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Terminus\Deploy;
use \Terminus\SiteWorkflow;
use \Terminus_Command;
use Terminus\Environments;
use Terminus\Collections\Environments;

class Site {
public $id;
Expand Down

0 comments on commit 6c3b766

Please sign in to comment.