Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3716775
D650.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D650.diff
View Options
diff --git a/src/Build/TriggerBuildFactory.php b/src/Build/TriggerBuildFactory.php
new file mode 100644
--- /dev/null
+++ b/src/Build/TriggerBuildFactory.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Keruald\DockerHub\Build;
+
+use Keruald\DockerHub\DockerHubImage;
+
+use GuzzleHttp\ClientInterface;
+
+use InvalidArgumentException;
+
+/**
+ * Allows to build several TriggerBuild instances
+ * with the same HTTP client and a collection of tokens.
+ */
+class TriggerBuildFactory {
+
+ ///
+ /// Private members, to pass to instances
+ ///
+
+ /**
+ * @var $array
+ */
+ private $tokens;
+
+ /**
+ * @var \GuzzleHttp\ClientInterface
+ */
+ private $client;
+
+ ///
+ /// Constructor
+ ///
+
+ /**
+ * Initializes a new instance of the TriggerBuildFactory object.
+ *
+ * @param \GuzzleHttp\ClientInterface $client HTTP client
+ * @param array $tokens The tokens for the image
+ */
+ public function __construct (ClientInterface $client, array $tokens) {
+ $this->client = $client;
+ $this->tokens = $tokens;
+ }
+
+ ///
+ /// Builder
+ ///
+
+ /**
+ * Builds a new TriggerBuild instance for the specified image.
+ *
+ * @param string $image The image name
+ * @return TriggerBuild
+ */
+ public function getForImage ($image) {
+ return new TriggerBuild(
+ DockerHubImage::loadFromSlashNotation($image),
+ $this->getToken($image),
+ $this->client
+ );
+ }
+
+ /**
+ * Sends to registry a payload to trigger a build for the specified image.
+ *
+ * @param string $image The image name
+ */
+ public function build ($image) {
+ $this->getForImage($image)->sendPayloadForAll();
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ protected function getToken ($image) {
+ if (array_key_exists($image, $this->tokens)) {
+ return $this->tokens[$image];
+ }
+
+ throw new InvalidArgumentException("No token found for image $image.");
+ }
+
+}
diff --git a/tests/Build/TriggerBuildFactoryTest.php b/tests/Build/TriggerBuildFactoryTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Build/TriggerBuildFactoryTest.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Keruald\DockerHub\Tests\Build;
+
+use Keruald\DockerHub\DockerHubImage;
+use Keruald\DockerHub\Build\TriggerBuild;
+use Keruald\DockerHub\Build\TriggerBuildFactory;
+
+use Keruald\DockerHub\Tests\WithMockHttpClient;
+
+class TriggerBuildFactoryTest extends \PHPUnit_Framework_TestCase {
+
+ use WithMockHttpClient;
+
+ /**
+ * @var Keruald\DockerHub\Build\TriggerBuildFactory
+ */
+ protected $factory;
+
+ public function setUp () {
+ $client = self::mockHttpClient();
+ $tokens = self::mockTokens();
+ $this->factory = new TriggerBuildFactory($client, $tokens);
+ }
+
+ /**
+ * @return array
+ */
+ private static function mockTokens () {
+ return [
+ "acme/foo" => "0000",
+ ];
+ }
+
+ public function testGetForImage () {
+ $this->assertInstanceOf(
+ TriggerBuild::class,
+ $this->factory->getForImage("acme/foo")
+ );
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testGetForImageWithoutToken () {
+ $this->factory->getForImage("acme/bar");
+ }
+
+ public function testBuild () {
+ // That returns void.
+ // We don't need to test the sendPayloadForAll method.
+ // So, we only need to check there is no exception or error.
+ $this->factory->build("acme/foo");
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Nov 6, 19:53 (17 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2232225
Default Alt Text
D650.diff (3 KB)
Attached To
Mode
D650: Add factory to generate TriggerBuild classes
Attached
Detach File
Event Timeline
Log In to Comment