Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3752626
D160.id385.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
D160.id385.diff
View Options
diff --git a/app/Console/Commands/PhabricatorGetProjectsMap.php b/app/Console/Commands/PhabricatorGetProjectsMap.php
--- a/app/Console/Commands/PhabricatorGetProjectsMap.php
+++ b/app/Console/Commands/PhabricatorGetProjectsMap.php
@@ -6,6 +6,7 @@
use Storage;
use Nasqueron\Notifications\Phabricator\ProjectsMap;
+use Nasqueron\Notifications\Services;
class PhabricatorGetProjectsMap extends Command {
/**
@@ -37,27 +38,14 @@
* @return mixed
*/
public function handle() {
- foreach ($this->getServicesCredentials() as $service) {
- if ($service->gate == "Phabricator") {
- $this->info("Querying projects map for " . $service->instance);
- $map = ProjectsMap::fetch($service->instance);
- $map->saveToCache();
- $this->table(
- ['PHID', 'Project name'],
- $map->toArray()
- );
- }
+ foreach (Services::getForGate('Phabricator') as $service) {
+ $this->info("Querying projects map for " . $service->instance);
+ $map = ProjectsMap::fetch($service->instance);
+ $map->saveToCache();
+ $this->table(
+ ['PHID', 'Project name'],
+ $map->toArray()
+ );
}
}
-
- /**
- * Gets service credentials
- *
- * @return stdClass the services credentials
- */
- protected function getServicesCredentials () {
- $path = config('services.gate.credentials');
- $data = json_decode(Storage::get($path));
- return $data->services;
- }
}
diff --git a/app/Http/Controllers/Gate/GateController.php b/app/Http/Controllers/Gate/GateController.php
--- a/app/Http/Controllers/Gate/GateController.php
+++ b/app/Http/Controllers/Gate/GateController.php
@@ -3,6 +3,7 @@
namespace Nasqueron\Notifications\Http\Controllers\Gate;
use Nasqueron\Notifications\Features;
+use Nasqueron\Notifications\Services;
use Nasqueron\Notifications\Http\Controllers\Controller;
use Report;
@@ -75,40 +76,12 @@
///
/**
- * Gets services credentials
- *
- * @return stdClass the services credentials
- */
- protected function getServicesCredentials () {
- $path = config('services.gate.credentials');
- $data = json_decode(Storage::get($path));
- return $data->services;
- }
-
- /**
- * Determines if a service definition matches this current gate and door.
- *
- * @param stdClass $service the service to check
- * @return true if the service matches our gate and door; otherwise, false.
- */
- protected function doesServiceMatch ($service) {
- return $service->gate == static::SERVICE_NAME
- && $service->door == $this->door;
- }
-
- /**
* Gets service credentials for this gate and door
*
* @return stdClass the service credentials
*/
public function getService () {
- foreach ($this->getServicesCredentials() as $service) {
- if ($this->doesServiceMatch($service)) {
- return $service;
- }
- }
-
- return null;
+ return Services::findServiceByDoor(static::SERVICE_NAME, $this->door);
}
/**
diff --git a/app/Phabricator/PhabricatorAPI.php b/app/Phabricator/PhabricatorAPI.php
--- a/app/Phabricator/PhabricatorAPI.php
+++ b/app/Phabricator/PhabricatorAPI.php
@@ -2,7 +2,7 @@
namespace Nasqueron\Notifications\Phabricator;
-use Storage;
+use Nasqueron\Notifications\Services;
class PhabricatorAPI {
@@ -46,11 +46,10 @@
* @return PhabricatorAPI|null A PhabricatorAPI instance for the project if found; otherwise, null.
*/
public static function forInstance ($instance) {
- $service = self::getServiceForInstance($instance);
+ $service = Services::findServiceByProperty('Phabricator', 'instance', $instance);
if ($service === null) {
throw new \RuntimeException("No credentials for Phabricator instance $instance.");
}
-
return new self($service->instance, $service->secret);
}
@@ -61,44 +60,13 @@
* @return PhabricatorAPI|null A PhabricatorAPI instance for the project if found; otherwise, null.
*/
public static function forProject ($project) {
- $service = self::getServiceForProject($project);
+ $service = Services::findServiceByDoor('Phabricator', $project);
if ($service === null) {
return null;
}
return new self($service->instance, $service->secret);
}
-
- ///
- /// Helper methods for static constructors
- ///
-
- private static function getServices () {
- $path = config('services.gate.credentials');
- $data = json_decode(Storage::get($path));
- return $data->services;
- }
-
- private static function getServiceForInstance ($instance) {
- foreach (self::getServices() as $service) {
- if ($service->gate === "Phabricator" && $service->instance === $instance) {
- return $service;
- }
- }
-
- return null;
- }
-
- private static function getServiceForProject ($project) {
- foreach (self::getServices() as $service) {
- if ($service->gate === "Phabricator" && $service->door === $project) {
- return $service;
- }
- }
-
- return null;
- }
-
///
/// Public methods
///
diff --git a/app/Services.php b/app/Services.php
new file mode 100644
--- /dev/null
+++ b/app/Services.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Nasqueron\Notifications;
+
+use Storage;
+
+class Services {
+
+ /**
+ * Gets the services found in credentials.json
+ *
+ * @return array
+ */
+ public static function get () {
+ $path = config('services.gate.credentials');
+ $data = json_decode(Storage::get($path));
+ return $data->services;
+ }
+
+ /**
+ * Gets all the services for a specific gate
+ *
+ * @param string $gate The gate (e.g. GitHub)
+ * @return array
+ */
+ public static function getForGate ($gate) {
+ return $services = [];
+
+ foreach (self::get() as $service) {
+ if ($service->gate === $gate) {
+ $services[] = $service;
+ }
+ }
+
+ return $services;
+ }
+
+
+ /**
+ * Gets the service for a specific gate and door
+ *
+ * @param string $gate The gate (e.g. GitHub)
+ * @param string $door The door (e.g. Nasqueron)
+ * @return stdClass|null The service information is found; otherwise, null.
+ */
+ public static function findServiceByDoor ($gate, $door) {
+ foreach (self::get() as $service) {
+ if ($service->gate === $gate && $service->door === $door) {
+ return $service;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Finds a service for a specific gate, property and value
+ *
+ * @param string $gate The gate (e.g. GitHub)
+ * @param string $property The property to check (e.g. instance)
+ * @param mixed $value The property value to find (e.g. 'http://devcentral.nasqueron.org')
+ * @return stdClass|null The service information is found; otherwise, null.
+ */
+ public static function findServiceByProperty ($gate, $property, $value) {
+ foreach (Services::get() as $service) {
+ if ($service->gate === $gate && $service->$property === $value) {
+ return $service;
+ }
+ }
+
+ return null;
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 18, 20:24 (21 h, 57 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2250934
Default Alt Text
D160.id385.diff (7 KB)
Attached To
Mode
D160: Services
Attached
Detach File
Event Timeline
Log In to Comment