Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

LaravelPHP artisan command to search the apibunny maze

View MazeCommand.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
<?php
use Guzzle\Http\Client;
use Guzzle\Http\Exception\BadResponseException;
use Illuminate\Console\Command;
use Illuminate\Foundation\Application;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class MazeCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'maze:search';
/**
* @var array
*/
protected $queue = [];
/**
* @var array
*/
protected $done = [];
/**
* @var string
*/
protected $baseUrl = 'http://apibunny.com/';
/**
* @var Illuminate\Foundation\Application
*/
protected $app;
/**
* @var Guzzle\Http\Client
*/
protected $client;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Search the apibunny.com maze';
/**
* Constructor injection
*
* @param Guzzle\Http\Client $client
*/
public function __construct(Client $client)
{
// Call the parent constructor.
parent::__construct();
// Assign injected dependencies.
$this->client = $client;
// Setup some stuff.
$this->init();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
// Ask the first cell id.
$id = $this->ask('What is the ID of the first cell?');
// Do the first request.
$user = $this->process($id);
while (is_null($user)) {
// Getting a random id from the queue and add it to the done list.
$this->done[] = $id = head(array_splice($this->queue, rand(0, count($this->queue) - 1), 1));
// Display some info.
$this->info("visiting $id");
// Get the new id.
$user = $this->process($id);
}
// Print out the winning info.
$this->info(json_encode($user, JSON_PRETTY_PRINT));
}
/**
* Get a cell and process the links to neighbouring cells.
*
* @param string $id
*/
protected function process($id)
{
// Do the request.
$result = $this->client->get("cells/$id")->send()->json();
// Loop result.
foreach (array_get($result, 'cells') as $cell) {
// If the exit link is present: exit.
if (array_key_exists('exit_link', $cell)) {
// Return the result.
return $this->post(array_get($cell, 'exit_link'));
}
// Add links to the queue.
$this->addLinks(array_get($cell, 'links', array()));
}
}
/**
* Add links to the queue.
*
* @param array $links
* @return void
*/
protected function addLinks(array $links)
{
// Loop the links.
foreach ($links as $name => $id) {
// If it hasn't been visited...
if (!in_array($id, $this->done) && !in_array($id, $this->queue) && $name != 'maze') {
// ... add it to the queue
$this->queue[] = $id;
}
}
}
/**
* Post to the exit link.
*
* @param string $href
*/
protected function post($href)
{
// Ask for the handle.
$handle = $this->ask('What is your twitter handle?');
// Remove the submit part.
$href = preg_replace('/\ \[submit\:.*\]$/', '', $href);
// Build json api post body
$body = [
'users' => [
['twitter_handle' => $handle]
]
];
// Send json.
$headers = ['Content-Type' => 'application/vnd.api+json'];
// Try posting the data.
try {
return $user = $this->client->post($href, $headers, json_encode($body))->send()->json();
} catch (BadResponseException $exception) {
return $user = $exception->getResponse()->json();
}
}
/**
* Init the Guzzle client.
*
* @return void
*/
protected function init()
{
$this->client->setBaseUrl($this->baseUrl);
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [];
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.