How to use CallbackToken class

Best Prophecy code snippet using CallbackToken

WebhookManager.php

Source:WebhookManager.php Github

copy

Full Screen

1<?php2namespace Bundle\Site\MarketPlace\Service;3use Bolt\Extension\BoltAuth\Auth\AccessControl\Session as AuthSession;4use Github\Client as GithubClient;5use Github\Exception\ApiLimitExceedException;6use Github\Exception\ExceptionInterface as GithubExceptionInterface;7use Silex\Application;8use Symfony\Component\HttpFoundation\Session\Session;9/**10 * Webhook manager class.11 *12 * @see https://developer.github.com/v3/repos/hooks/#create-a-hook13 *14 * @author Gawain Lynch <gawain.lynch@gmail.com>15 */16class WebhookManager17{18 /** @var Session */19 protected $session;20 /** @var Application */21 private $app;22 /**23 * Constructor.24 *25 * @param Application $app26 */27 public function __construct(Application $app)28 {29 $this->app = $app;30 $this->session = $app['session'];31 }32 /**33 * @param string $username34 * @param string $repository35 *36 * @return array37 */38 public function getRateLimit($username, $repository)39 {40 if (!$client = $this->getGitHubClient()) {41 return [];42 }43 /** @var \Github\Api\RateLimit $apiRateLimit */44 $apiRateLimit = $client->api('rate_limit');45 $rateLimits = $apiRateLimit->getRateLimits();46 return $rateLimits;47 }48 /**49 * @param string $username50 * @param string $repository51 * @param string $callbackToken52 *53 * @return bool54 */55 public function createWebhook($username, $repository, $callbackToken)56 {57 if (!$client = $this->getGitHubClient()) {58 return false;59 }60 if ($this->hasWebhook($username, $repository, $callbackToken)) {61 $this->session->getFlashBag()->add('error', 'Webhook already exists');62 return false;63 }64 /** @var \Github\Api\Repo $apiRepo */65 $apiRepo = $client->api('repo');66 try {67 $result = $apiRepo->hooks()->create($username, $repository, $this->getWebhookParameters($callbackToken));68 } catch (\Exception $e) {69 $this->handleException($e);70 return false;71 }72 if (!$this->isHookValid((array) $result, $callbackToken)) {73 $this->session->getFlashBag()->add('error', sprintf('Response invalid creating webhook: %s', json_encode($result)));74 return false;75 }76 $this->session->getFlashBag()->add('success', 'Successfully created webhook!');77 $this->session->set('pending-' . $callbackToken, $callbackToken);78 return true;79 }80 /**81 * Check if the webhook exists.82 *83 * @param string $username84 * @param string $repository85 * @param string $callbackToken86 *87 * @return bool88 */89 public function hasWebhook($username, $repository, $callbackToken)90 {91 if (!$client = $this->getGitHubClient()) {92 return false;93 }94 /** @var \Github\Api\Repo $apiRepo */95 $apiRepo = $client->api('repo');96 try {97 $hooks = $apiRepo->hooks()->all($username, $repository);98 } catch (\Exception $e) {99 $this->handleException($e);100 return false;101 }102 foreach ($hooks as $hook) {103 $isType = $hook['type'] === 'Repository';104 $isEvents = array_diff($hook['events'], $this->getHookEvents()) === [];105 $isConfigJson = $hook['config']['content_type'] === 'json';106 $isSslCertCheck = (int) $hook['config']['insecure_ssl'] === 0;107 $isValidCallback = $hook['config']['url'] === $this->getCallbackUrl($callbackToken);108 if ($isType && $isEvents && $isConfigJson && $isSslCertCheck && $isValidCallback) {109 return true;110 }111 }112 return false;113 }114 /**115 * Ping our webhook to see if it's working.116 *117 * @param string $username118 * @param string $repository119 * @param string $id120 *121 * @return bool122 */123 public function pingWebhook($username, $repository, $id)124 {125 if (!$client = $this->getGitHubClient()) {126 return false;127 }128 /** @var \Github\Api\Repo $apiRepo */129 $apiRepo = $client->api('repo');130 try {131 $apiRepo->hooks()->ping($username, $repository, $id);132 } catch (\Exception $e) {133 $this->handleException($e);134 return false;135 }136 $this->session->getFlashBag()->add('success', 'Successfully pinged webhook!');137 return true;138 }139 /**140 * Remove our webhook.141 *142 * @param string $username143 * @param string $repository144 * @param string $id145 *146 * @return bool147 */148 public function removeWebhook($username, $repository, $id)149 {150 if (!$client = $this->getGitHubClient()) {151 return false;152 }153 /** @var \Github\Api\Repo $apiRepo */154 $apiRepo = $client->api('repo');155 try {156 $apiRepo->hooks()->remove($username, $repository, $id);157 } catch (\Exception $e) {158 $this->handleException($e);159 return false;160 }161 return true;162 }163 /**164 * Return an authorised GitHub client object.165 *166 * @return GithubClient|null167 */168 protected function getGitHubClient()169 {170 if (!$accessToken = $this->getAccessToken()) {171 return null;172 }173 /** @var GithubClient $client */174 $client = $this->app['github.api.client'];175 $client->authenticate($accessToken, $accessToken, GithubClient::AUTH_HTTP_TOKEN);176 return $client;177 }178 /**179 * Return the in-use access token from the Auth session.180 *181 * @return \League\OAuth2\Client\Token\AccessToken182 */183 protected function getAccessToken()184 {185 /** @var AuthSession $membersSession */186 $membersSession = $this->app['auth.session'];187 if ($membersSession->hasAuthorisation() === false) {188 return null;189 }190 return $membersSession->getAuthorisation()->getAccessToken('github');191 }192 /**193 * Checks a hook array for validity.194 *195 * @param array $hook196 * @param string $token197 *198 * @return bool199 */200 protected function isHookValid(array $hook, $token)201 {202 if (!isset($hook['type']) || $hook['type'] !== 'Repository') {203 return false;204 }205 if (!isset($hook['name']) || $hook['name'] !== 'web') {206 return false;207 }208 if (!isset($hook['active']) || !$hook['active']) {209 return false;210 }211 if (!isset($hook['config']['content_type']) || $hook['config']['content_type'] !== 'json') {212 return false;213 }214 if (!isset($hook['config']['insecure_ssl']) || $hook['config']['insecure_ssl']) {215 return false;216 }217 if (!isset($hook['config']['url']) || $hook['config']['url'] !== $this->getCallbackUrl($token)) {218 return false;219 }220 if (!isset($hook['events']) || array_diff($hook['events'], $this->getHookEvents()) !== []) {221 return false;222 }223 return true;224 }225 /**226 * Retrun an array of parameters.227 *228 * @param string $callbackToken229 *230 * @return array231 */232 protected function getWebhookParameters($callbackToken)233 {234 return [235 'name' => 'web',236 'active' => true,237 'events' => $this->getHookEvents(),238 'config' => [239 'content_type' => 'json',240 'insecure_ssl' => 0,241 'url' => $this->getCallbackUrl($callbackToken),242 ],243 ];244 }245 /**246 * Return a valid callback URL.247 *248 * @param string $callbackToken249 *250 * @return string251 */252 protected function getCallbackUrl($callbackToken)253 {254 return sprintf('%s/hook?token=%s', $this->app['resources']->getUrl('hosturl'), $callbackToken);255 }256 /**257 * The events our hooks wants to trigger on.258 *259 * @return array260 */261 protected function getHookEvents()262 {263 return [264 'create',265 'delete',266 'push',267 'release',268 ];269 }270 /**271 * @param \Exception $e272 */273 protected function handleException(\Exception $e)274 {275 if ($e instanceof ApiLimitExceedException) {276 $this->session->getFlashBag()->add('error', 'GitHub API request limit exceeded.');277 } elseif ($e instanceof GithubExceptionInterface) {278 if ($e->getCode() === 404) {279 $this->session->getFlashBag()->add('error', 'Authenticating token failure with GitHub');280 } else {281 $this->session->getFlashBag()->add('error', sprintf('GitHub API exception: %s', $e->getMessage()));282 }283 } else {284 $this->session->getFlashBag()->add('error', sprintf('Exception type: %s', get_class($e)));285 $this->session->getFlashBag()->add('error', sprintf('Exception occurred creating webhook: %s', $e->getMessage()));286 }287 }288}...

Full Screen

Full Screen

CallbackResult.php

Source:CallbackResult.php Github

copy

Full Screen

...21 /**22 * @param string $callbackToken23 * @return CallbackResult24 */25 public function setCallbackToken($callbackToken)26 {27 $this->callbackToken = $callbackToken;28 return $this;29 }30 /**31 * @return string32 */33 public function getEndPoint()34 {35 return "/api/Property/{$this->accessKey}/CallbackResult/{$this->callbackToken}";36 }37 /**38 * @return string39 */...

Full Screen

Full Screen

CallbackToken

Using AI Code Generation

copy

Full Screen

1require 'CallbackToken.php';2require 'Prophecy.php';3$callbackToken = new CallbackToken();4$prophecy = new Prophecy();5$callbackToken->callback();6$prophecy->prophecy();

Full Screen

Full Screen

CallbackToken

Using AI Code Generation

copy

Full Screen

1use Prophecy\Argument;2use Prophecy\Prophecy\CallbackToken;3{4 public function testMethod($arg)5 {6 }7}8$prophet = new Prophet;9$mock = $prophet->prophesize('TestClass');10$mock->testMethod(Argument::that(function($arg) {11 return $arg === 'foo';12}))->willReturn('bar');13$mock->testMethod(new CallbackToken(function($arg) {14 return $arg === 'foo';15}))->willReturn('bar');16use Prophecy\Argument;17use Prophecy\Argument\Token\CallbackToken;18{19 public function testMethod($arg)20 {21 }22}23$prophet = new Prophet;24$mock = $prophet->prophesize('TestClass');25$mock->testMethod(Argument::that(function($arg) {26 return $arg === 'foo';27}))->willReturn('bar');28$mock->testMethod(new CallbackToken(function($arg) {29 return $arg === 'foo';30}))->willReturn('bar');31use Prophecy\Argument;32use Prophecy\Argument\Token\CallbackToken;33{34 public function testMethod($arg)35 {36 }37}38$prophet = new Prophet;39$mock = $prophet->prophesize('TestClass');40$mock->testMethod(Argument::that(function($arg) {41 return $arg === 'foo';42}))->willReturn('bar');43$mock->testMethod(new CallbackToken(function($arg) {44 return $arg === 'foo';45}))->willReturn('bar');46use Prophecy\Argument;47use Prophecy\Argument\Token\CallbackToken;48{49 public function testMethod($arg)50 {51 }52}53$prophet = new Prophet;54$mock = $prophet->prophesize('TestClass');55$mock->testMethod(Argument::that(function($arg) {56 return $arg === 'foo';57}

Full Screen

Full Screen

CallbackToken

Using AI Code Generation

copy

Full Screen

1require_once 'CallbackToken.php';2$token = new CallbackToken();3$token->setCallback(function($value) {4 return $value > 2;5});6$token->scoreArgument(3);7echo $token->scoreArgument(3);8echo $token->scoreArgument(1);9require_once 'Argument.php';10$token = new Prophecy\Argument\Token\CallbackToken();11$token->setCallback(function($value) {12 return $value > 2;13});14$token->scoreArgument(3);15echo $token->scoreArgument(3);16echo $token->scoreArgument(1);

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Prophecy automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in CallbackToken

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful