<?php

namespace Bolt\Extension\Ginger\Squarepay\Twig;

use Silex\Application;

/**
 * Twig runtime class that will only be invoked when one of its functions or
 * filters are used.
 *
 * @author Richard Knight <rich@apewave.com>
 */
class SquareTwigRuntime
{
    private $apiClient;

	public function __construct($apiClient, $twig, $config)
	{
		$this->apiClient = $apiClient;
		$this->twig = $twig;
		$this->config = $config;
	}

	public function getCatalog()
	{
		try {
			$catalogApi = new \SquareConnect\Api\CatalogApi($this->apiClient);
			$products = $catalogApi->listCatalog();
		} catch (Exception $e) {
			error_log($e->getMessage());
			return 'ERROR: '.$e->getMessage();
		}
		$response = [];
		foreach($products['objects'] as $p) {
			if ($p['type'] === 'ITEM') {
				array_push($response, $p);
			}
		}
		return $response;
	}
	
	public function displayCCForm() {
		$context = $this->config + [
			'testmode' => true
		];
		return $this->twig->render('@Squarefront/cardentryform.twig', $context);
	}

}