RemoteClientExtension.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace WebserviceBundle\Twig;
  3. use WebserviceBundle\Services\Webservice;
  4. use WebserviceBundle\Utils\HttpUtils;
  5. class RemoteClientExtension extends \Twig_Extension
  6. {
  7. /**
  8. * @var Webservice
  9. */
  10. protected $webservice;
  11. /**
  12. * @var string
  13. */
  14. protected $remoteClientCreateUrl;
  15. /**
  16. * @param Webservice $webservice
  17. * @param string $remoteClientCreateUrl
  18. */
  19. public function __construct(Webservice $webservice, $remoteClientCreateUrl)
  20. {
  21. $this->webservice = $webservice;
  22. $this->remoteClientCreateUrl = $remoteClientCreateUrl;
  23. }
  24. /**
  25. * @return array
  26. */
  27. public function getFunctions()
  28. {
  29. return array(
  30. new \Twig_SimpleFunction('remote_client', array($this, 'getClient')),
  31. new \Twig_SimpleFunction('remote_client_url', array($this, 'getClientCreateUrl')),
  32. );
  33. }
  34. /**
  35. * @param int $clientId
  36. *
  37. * @return string
  38. */
  39. public function getClient($clientId)
  40. {
  41. return $this->webservice->getById('client', $clientId);
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getClientCreateUrl()
  47. {
  48. $url = HttpUtils::cleanUrl($this->remoteClientCreateUrl);
  49. if ($url == '' || filter_var($url, FILTER_VALIDATE_URL) === false) {
  50. return '#';
  51. }
  52. return $url;
  53. }
  54. /**
  55. * @return string
  56. */
  57. public function getName()
  58. {
  59. return 'remote_client_extension';
  60. }
  61. }