/** * @param ConnectionInterface $conn * @param string $id * @param string $topic * @param WampRequest $request * @param array $params */ public function dispatch(ConnectionInterface $conn, $id, $topic, WampRequest $request, array $params) { $callback = $request->getRoute()->getCallback(); try { $procedure = $this->rpcRegistry->getRpc($callback); } catch (\Exception $e) { $conn->callError($id, $topic, $e->getMessage(), ['rpc' => $topic, 'request' => $request]); return; } $method = $this->toCamelCase($request->getAttributes()->get('method')); $result = null; try { $result = call_user_func([$procedure, $method], $conn, $request, $params); } catch (\Exception $e) { $conn->callError($id, $topic, $e->getMessage(), ['code' => $e->getCode(), 'rpc' => $topic, 'params' => $params, 'request' => $request]); return; } if ($result === null) { $result = false; } if ($result) { if ($result instanceof RpcResponse) { $result = $result->getData(); } elseif (!is_array($result)) { $result = [$result]; } $conn->callResult($id, $result); return; } elseif ($result === false) { $conn->callError($id, $topic, 'RPC Error', ['rpc' => $topic, 'params' => $params, 'request' => $request]); } $conn->callError($id, $topic, 'Unable to find that command', ['rpc' => $topic->getId(), 'params' => $params, 'request' => $request]); return; }
/** * @param Topic $topic * @param WampRequest $request * @param array|string $data * @param string $provider The name of pusher who push the data */ public function onPush(Topic $topic, WampRequest $request, $data, $provider) { $this->logger->info("onPush -> " . $topic->getId()); /* @var $slack \FourPixelsBundle\Entity\Slack */ /* @var $slackRequest \FourPixelsBundle\Entity\SlackRequest */ /* @var $slackTeamTreeHouse \FourPixelsBundle\Entity\SlackTeamTreeHouse */ $this->em->clear(); $slackRequest = $this->em->getRepository('FourPixelsBundle:SlackRequest')->find($request->getAttributes()->get('slackRequest')); $slack = $this->em->getRepository('FourPixelsBundle:Slack')->find($request->getAttributes()->get('slack')); $scoreTable = []; if ($slack->getId() === $slackRequest->getSlack()->getId()) { $client = new \Guzzle\Http\Client(); $explode = explode(' ', $slackRequest->getText()); $globalShowMode = 'in_channel'; // ephemeral OR in_channel switch ($explode[0]) { case 'help': $myArray = ['username' => '4pixels', "icon_url" => "https://slack.com/img/icons/app-57.png", "icon_emoji" => ":ghost:", "response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/list", "title" => "LIST", "text" => "/teamtreehouse list \n Displays a list of all the Team Tree House usernames participaiting in the tournament. \n *Example:* _/teamtreehouse list_", "mrkdwn_in" => ['text'], "color" => "#3F2860"], ["fallback" => "please visit https://4pixels.co/api-help/add", "title" => "ADD", "text" => "/teamtreehouse add `TeamTreeHouse username` \n adds a Team Tree House username to the tournament. \n *Example:* _/teamtreehouse add jasoncameron_", "mrkdwn_in" => ['text'], "color" => "#F35A00"], ["fallback" => "please visit https://4pixels.co/api-help/remove", "title" => "REMOVE", "text" => "/teamtreehouse remove `TeamTreeHouse username` \n Delete a Team Tree House username from the tournament. \n *Example:* _/teamtreehouse remove jasoncameron_", "mrkdwn_in" => ['text'], "color" => "#B81D18"], ["fallback" => "please visit https://4pixels.co/api-help/remove", "title" => "SHOW", "text" => "/teamtreehouse show `TeamTreeHouse username` \n Show a single Team Tree House username score. \n *Example:* _/teamtreehouse show jasoncameron_", "mrkdwn_in" => ['text'], "color" => "#1253A4"], ["fallback" => "please visit https://4pixels.co/api-help/code", "title" => "CODE", "text" => "" . "/teamtreehouse code \n Displays a ranking list of Team Tree House usernames tournament. \n *Example:* _/teamtreehouse code_ \n\n" . "/teamtreehouse code `TeamTreeHouse language` \n Displays a ranking list of Team Tree House usernames tournament based on one lenguage. \n *Example:* _/teamtreehouse code javascript_ \n\n" . "/teamtreehouse code `TeamTreeHouse languages` \n Displays a ranking list of Team Tree House usernames tournament based on many lenguage. \n *Example:* _/teamtreehouse code javascript, php, html_", "mrkdwn_in" => ['text'], "color" => "#75A3D1"]]]; break; case 'show': // /teamtreehouse show jasoncameron $username = $explode[1]; //MOST VALIDATE THERE ARE NO MORE PARAMETERS THAN 2 try { $teamTreeHouseResponse = $client->get('https://teamtreehouse.com/' . $username . '.json')->send(); $slackTeamTreeHouse = new \FourPixelsBundle\Entity\SlackTeamTreeHouse(); $slackTeamTreeHouse->setContent($teamTreeHouseResponse->json()); $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/show", "title" => "SHOW " . $slackTeamTreeHouse->getName() . " (" . $slackTeamTreeHouse->getProfileName() . ")", "color" => "#75A3D1", "thumb_url" => $slackTeamTreeHouse->getGravatarUrl(), "fields" => $slackTeamTreeHouse->getPoints(true)]]]; } catch (ClientErrorResponseException $exception) { $responseBody = $exception->getResponse()->getBody(true); $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/remove", "title" => "SHOW " . $username . " - Ups !!! :panda_face: Panda Trouble", "text" => "Username " . $responseBody . " on Team Tree House", "mrkdwn_in" => ['text'], "color" => "#D40E52"]]]; } break; case 'code': $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/code", "title" => "Feature CODE", "text" => "The panda is busy right know thinking in other stuff. He will develop this feature soon.", "mrkdwn_in" => ['text'], "color" => "#D40E52"]]]; break; case 'list': $slackTeamTreeHouseList = $slack->getSlackTeamTreeHouseList(); $text = ""; foreach ($slackTeamTreeHouseList as $slackTeamTreeHouse) { $text .= $slackTeamTreeHouse->getName() . ' (' . $slackTeamTreeHouse->getProfileName() . ")\n"; } $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/list", "title" => "LIST - List size: " . count($slackTeamTreeHouseList), "text" => $text, "color" => "#75A3D1"]]]; break; case 'add': $username = $explode[1]; //MOST VALIDATE THERE ARE NO MORE PARAMETERS THAN 2 try { $teamTreeHouseResponse = $client->get('https://teamtreehouse.com/' . $username . '.json')->send(); $slackTeamTreeHouse = new \FourPixelsBundle\Entity\SlackTeamTreeHouse(); $slackTeamTreeHouse->setContent($teamTreeHouseResponse->json()); $slackTeamTreeHouse->setSlack($slack); $this->em->persist($slackTeamTreeHouse); $this->em->flush(); $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/add", "title" => "ADD " . $slackTeamTreeHouse->getName() . " (" . $slackTeamTreeHouse->getProfileName() . ")", "text" => "User has been added successfuly", "color" => "#75A3D1", "thumb_url" => $slackTeamTreeHouse->getGravatarUrl()]]]; } catch (ClientErrorResponseException $exception) { $responseBody = $exception->getResponse()->getBody(true); $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/add", "title" => "ADD " . $username . " - Ups !!! :panda_face: Panda Trouble ", "text" => "Username " . $responseBody . " on Team Tree House", "color" => "#D40E52"]]]; } catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) { $this->em->clear(); $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/add", "title" => "ADD " . $username . " - Ups !!! :panda_face: Panda Warning ", "text" => "Username " . $username . " is already added", "color" => "#FF9900"]]]; } break; case 'remove': $username = $explode[1]; $slackTeamTreeHouse = $this->em->getRepository('FourPixelsBundle:SlackTeamTreeHouse')->findOneBy(['slack' => $slack->getId(), 'profile_name' => $username]); if (!is_null($slackTeamTreeHouse)) { try { $name = $slackTeamTreeHouse->getName(); $gravatarUrl = $slackTeamTreeHouse->getGravatarUrl(); $this->em->remove($slackTeamTreeHouse); $this->em->flush(); $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/remove", "title" => "REMOVE " . $name . " (" . $username . ")", "text" => "User has been removed from the list successfuly", "color" => "#75A3D1", "thumb_url" => $gravatarUrl]]]; } catch (\Exception $e) { $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/remove", "title" => "REMOVE " . $username . " - Ups !!! :panda_face: Panda Warning ", "text" => "The was a problem removing the user from the list. ", "color" => "#FF9900"]]]; } } else { $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help/remove", "title" => "REMOVE " . $username . " - Ups !!! :panda_face: Panda Trouble ", "text" => "Username was not found on the list. \n You can view the list with: /teamtreehouse list", "color" => "#D40E52"]]]; } break; default: $slackTeamTreeHouseList = $slackRequest->getSlack()->getSlackTeamTreeHouseList(); foreach ($slackTeamTreeHouseList as $slackTeamTreeHouse) { $teamTreeHouseResponse = $client->get('https://teamtreehouse.com/' . $slackTeamTreeHouse->getProfileName() . '.json')->send(); $slackTeamTreeHouse->setContent($teamTreeHouseResponse->json()); $this->em->persist($slackTeamTreeHouse); $scoreTable[$slackTeamTreeHouse->getName() . ' (' . $slackTeamTreeHouse->getProfileName() . ')'] = $slackTeamTreeHouse->getTotal(); } $this->em->flush(); $resultBool = arsort($scoreTable); $text = ""; foreach ($scoreTable as $key => $value) { $text .= $value . " \t \t " . $key . " \n "; } $myArray = ["response_type" => $globalShowMode, "attachments" => [["fallback" => "please visit https://4pixels.co/api-help", "title" => "RANKING NEWS ", "text" => $text, "color" => "#75A3D1"]]]; break; } $requestForSlack = $client->post($slackRequest->getResponseUrl(), [], ['payload' => json_encode($myArray)]); $response = $requestForSlack->send(); } $topic->broadcast(['msg' => 'DONE']); }