Skip to content

Commit

Permalink
Adds machine token commands for 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronan Dowling committed Sep 14, 2016
1 parent ce190dd commit 34cd341
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 3 deletions.
9 changes: 9 additions & 0 deletions bin/terminus.php
Expand Up @@ -15,10 +15,13 @@
use League\Container\Container;
use Pantheon\Terminus\Config;
use Pantheon\Terminus\Runner;
use Pantheon\Terminus\Session\Session;
use Pantheon\Terminus\Session\SessionAwareInterface;
use Pantheon\Terminus\Terminus;
use Robo\Robo;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Terminus\Caches\FileCache;

// Initializing the Terminus application
$config = new Config();
Expand All @@ -30,6 +33,12 @@
$output = new ConsoleOutput();
Robo::configureContainer($container, $config, $input, $output, $application);

$container->share('fileCache', FileCache::class);
$container->share('session', Session::class)
->withArgument('fileCache');
$container->inflector(SessionAwareInterface::class)
->invokeMethod('setSession', ['session']);

// Running Terminus
$runner = new Runner($container);
$status_code = $runner->run($input, $output);
Expand Down
22 changes: 21 additions & 1 deletion src/Commands/MachineToken/DeleteCommand.php
Expand Up @@ -3,6 +3,7 @@
namespace Pantheon\Terminus\Commands\MachineToken;

use Pantheon\Terminus\Commands\TerminusCommand;
use Terminus\Exceptions\TerminusException;

class DeleteCommand extends TerminusCommand
{
Expand All @@ -15,14 +16,33 @@ class DeleteCommand extends TerminusCommand
* Removes a machine token from the logged-in user's account
*
* @name machine-token:delete
* @param string $machine_token_id The ID of the machine token to be deleted
* @throws \Terminus\Exceptions\TerminusException
* @aliases mt:delete
*
* @param machine_token_id The ID of the machine token to be deleted
* @usage terminus machine-token:delete <machine_token_id>
* Removes the given machine token from the user's account
*/
public function delete($machine_token_id) {
$user = $this->session()->getUser();

// Find the token. Will throw an exception if it doesn't exist.
$machine_token = $user->machine_tokens->get($machine_token_id);
if (empty($machine_token)) {
throw new TerminusException('There are no machine tokens with the id {id}.', compact($machine_token_id));
}
$name = $machine_token->get('device_name');

// Confirm the delete.
$this->confirm(sprintf('Are you sure you want to delete "%s"?', $name));

$this->log()->info('Deleting {name} ...', ['name' => $name]);
$response = $machine_token->delete();
if ($response['status_code'] == 200) {
$this->log()->info('Deleted {name}!', ['name' => $name]);
} else {
throw new TerminusException('There was an problem deleting the machine token.');
}
}

}
22 changes: 21 additions & 1 deletion src/Commands/MachineToken/ListCommand.php
Expand Up @@ -3,6 +3,8 @@
namespace Pantheon\Terminus\Commands\MachineToken;

use Pantheon\Terminus\Commands\TerminusCommand;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;


class ListCommand extends TerminusCommand
{
Expand All @@ -19,9 +21,27 @@ class ListCommand extends TerminusCommand
*
* @usage terminus machine-token:list
* Lists your user's machine tokens
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
*/
public function listTokens() {
public function listTokens($options = ['format' => 'table', 'fields' => '']) {
$user = $this->session()->getUser();

$machine_tokens = $user->machine_tokens->all();
$data = array();
foreach ($machine_tokens as $id => $machine_token) {
$data[] = array(
'id' => $machine_token->id,
'device_name' => $machine_token->get('device_name'),
);
}

if (count($data) == 0) {
$this->log()->warning('You have no machine tokens.');
}

// Return the output data.
return new RowsOfFields($data);
}

}
5 changes: 4 additions & 1 deletion src/Commands/TerminusCommand.php
Expand Up @@ -3,6 +3,8 @@
namespace Pantheon\Terminus\Commands;

use Pantheon\Terminus\Config;
use Pantheon\Terminus\Session\SessionAwareInterface;
use Pantheon\Terminus\Session\SessionAwareTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
Expand All @@ -12,11 +14,12 @@
use Robo\Common\IO;
use Terminus\Models\Auth;

abstract class TerminusCommand implements IOAwareInterface, LoggerAwareInterface, ConfigAwareInterface
abstract class TerminusCommand implements IOAwareInterface, LoggerAwareInterface, ConfigAwareInterface, SessionAwareInterface
{
use LoggerAwareTrait;
use ConfigAwareTrait;
use IO;
use SessionAwareTrait;

/**
* TerminusCommand constructor
Expand Down
106 changes: 106 additions & 0 deletions src/Session/Session.php
@@ -0,0 +1,106 @@
<?php

namespace Pantheon\Terminus\Session;

use Terminus\Caches\FileCache;
use Terminus\Models\User;

class Session {
/**
* @var FileCache
*/
protected $cache;

/**
* @var object
*/
protected $data;

/**
* Instantiates object, sets session data
*/
public function __construct($fileCache) {
$this->cache = $fileCache;
$session = $this->cache->getData('session');
$this->data = $session;
if (empty($session)) {
$this->data = new \stdClass();
}
}

/**
* Removes the session from the cache
*
* @return void
*/
public function destroy() {
$this->cache->remove('session');
}

/**
* Returns given data property or default if DNE.
*
* @param string $key Name of property to return
* @param mixed $default Default return value in case property DNE
* @return mixed
*/
public function get($key = 'session', $default = false) {
if (isset($this->data) && isset($this->data->$key)) {
return $this->data->$key;
}
return $default;
}

/**
* Retrieves session data
*
* @return object
*/
public function getData() {
return $this->data;
}

/**
* Sets a keyed value to be part of the data property object
*
* @param string $key Name of data property
* @param mixed $value Value of property to set
* @return Session
*/
public function set($key, $value = null) {
$this->data->$key = $value;
return $this;
}

/**
* Saves session data to cache
*
* @param array $data Session data to save
* @return bool
*/
public function setData($data) {
if (empty($data)) {
return false;
}
$cache = new FileCache();
$cache->putData('session', $data);

$this->data->set('data', $data);
foreach ($data as $k => $v) {
$this->set($k, $v);
}
return true;
}

/**
* Returns a user with the current session user id
* @return \Terminus\Models\User [user] $session user
*/
public function getUser() {
$user_uuid = $this->get('user_uuid');
$user = new User((object)array('id' => $user_uuid));
return $user;
}

}
25 changes: 25 additions & 0 deletions src/Session/SessionAwareInterface.php
@@ -0,0 +1,25 @@
<?php

namespace Pantheon\Terminus\Session;


/**
* Interface SessionAwareInterface
* @package Pantheon\Terminus\Session
*
* Provides an interface for direct injection of the session helper.
*/
interface SessionAwareInterface {

/***
* @param Session $session
* @return void
*/
public function setSession(Session $session);

/**
* @return Session
*/
public function session();

}
38 changes: 38 additions & 0 deletions src/Session/SessionAwareTrait.php
@@ -0,0 +1,38 @@
<?php

namespace Pantheon\Terminus\Session;


/**
* Provides the basic properties needed to fulfill the SessionAwareInterface.
*
* Class SessionAwareTrait
* @package Pantheon\Terminus\Session
*/
trait SessionAwareTrait {

/**
* @var Session
*/
protected $session;

/**
* @inheritdoc
*/
public function setSession(Session $session)
{
$this->session = $session;

return $this;
}

/**
* @inheritdoc
*/
public function session()
{
return $this->session;
}


}
23 changes: 23 additions & 0 deletions tests/active_features/machine-token.feature
@@ -0,0 +1,23 @@
Feature: Machine tokens command
In order to manage my devices
As a user
I need to be able to view and delete my machine tokens.

Background: I am logged in
Given I am authenticated

@vcr machine-tokens_list
Scenario: List the machine tokens
When I run "terminus machine-token:list"
Then I should get:
"""
[[machine_token_id]]
"""

@vcr machine-tokens_delete
Scenario: Delete machine token
When I run "terminus machine-token:delete [[machine_token_id]] --yes"
Then I should get:
"""
Deleted [[machine_token_device]]!
"""
@@ -0,0 +1,51 @@
<?php
/**
* @file
* Contains Pantheon\Terminus\UnitTests\Commands\Auth\MachineTokenCommandTest
*/


namespace Pantheon\Terminus\UnitTests\Commands\Auth;


use Pantheon\Terminus\Session\Session;
use Psr\Log\NullLogger;
use Terminus\Collections\MachineTokens;
use Terminus\Models\User;

abstract class MachineTokenCommandTest extends \PHPUnit_Framework_TestCase {
protected $session;
protected $machine_tokens;
protected $user;
protected $logger;
protected $command;

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->machine_tokens = $this->getMockBuilder(MachineTokens::class)
->disableOriginalConstructor()
->getMock();

$this->user = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->getMock();
$this->user->machine_tokens = $this->machine_tokens;

$this->session = $this->getMockBuilder(Session::class)
->disableOriginalConstructor()
->getMock();

$this->session->method('getUser')
->willReturn($this->user);

$this->logger = $this->getMockBuilder(NullLogger::class)
->setMethods(array('log'))
->getMock();


}
}

0 comments on commit 34cd341

Please sign in to comment.