<commit_before><?php
namespace App\Services\RCO;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Collection;
use stdClass;

use App\Models\Organization;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
/**
 * This service interfaces with external API's that will allow it to convert a set of GPS coordinates
 * for a Philadelphia address into a set of Registered Community Organizations
 */
class GISRCOTranslationService implements RCOTranslationService
{
    /**
     * Given a specific point on a map, this method finds the Registered Community Organizations
     * that have that point within them by utilizing an external API
     *
     * @param stdClass $coordinates An object with 'latitude' and 'longitude' properties
     * @return Collection The set of Registered Community Organizations that a point belongs to
     * @throws Exception If the external service causes an issue
     */
    public function getRCOListForGPS(stdClass $coordinates): Collection
    {
        $rcos = [];
        $coordinatePair = sprintf('{"y":"%s","x":"%s"}', $coordinates->latitude, $coordinates->longitude);
        if (!Cache::has($coordinatePair)) {
            $api = env('RCO_API_ADDRESS');
            if (!$api) {
                throw new Exception('RCO_API_ADDRESS not set up: See documentation for setup under External Resources');
            }

            $client = new Client([
                // Base URI is used with relative requests
                'base_uri' => $api,
                // You can set any number of default request options.
                'timeout'  => 2.0,
            ]);

            try {
                set_time_limit(0);
                $queryParams = [
                    'where' => '1=1',
                    'geometry' => $coordinatePair,
                    'geometryType' => 'esriGeometryPoint',
                    'inSR' => 4326,
                    'spatialRel' => 'esriSpatialRelWithin',
                    'resultType' => 'none',
                    'distance' => '0.0',
                    'units' => 'esriSRUnit_Meter',
                    'returnGeodetic' => 'false',
                    'outFields' => '*',
                    'returnGeometry' => 'true',
                    'returnCentroid' => 'false',
                    'multipatchOption' => 'xyFootprint',
                    'maxAllowableOffset' => '',
                    'geometryPrecision' => '',
                    'outSR' => 4326,
                    'returnIdsOnly' => 'false',
                    'returnCountOnly' => 'false',
                    'returnExtentOnly' => 'false',
                    'returnDistinctValues' => 'false',
                    'orderByFields' => '',
                    'groupByFieldsForStatistics' => '',
                    'outStatistics' => '',
                    'resultOffset' => '',
                    'resultRecordCount' => '',
                    'returnZ' => 'false',
                    'returnM' => 'false',
                    'quantizationParameters' => '',
                    'sqlFormat' => 'none',
                    'f' => 'pgeojson',
                ];
                /** @var ResponseInterface $response */
                $response = $client->get('0/query', [
                    'query' => $queryParams,
                    'connect_timeout' => 20
                ]);

                if ($response->getStatusCode() === 200) {
                    $json = json_decode($response->getBody()->getContents());
                    foreach ($json->features as $rco) {
                        $current = $rco->properties;
                        $current->geometry = $rco->geometry;
                        $current = $this->sanitizeRCO($current);
                        Cache::put("rco_".$current->id, $current, Carbon::today()->endOfDay());
                        $rcos[] = $current;
                    }
                } else {
                    // We didn't get a success, handle exception
                    throw $this->generateException($response);
                }
                Cache::put($coordinatePair, $rcos, Carbon::today()->endOfDay());
            } catch (Exception $e) {
                // Something bad happened during the
                throw $e;
            }
        } else {
            $rcos = Cache::get($coordinatePair);
        }
        return collect($rcos);
    }

    /**
     * Given the local ID for an RCO, get any information that should be associated with it
     * @param  int    $id The ID of the object we're looking for
     * @return stdClass     The description of the RCO
     */
    public function getRCO(int $id): stdClass
    {
        $rco = null;

        if (!Cache::has("rco_$id")) {
            $api = env('RCO_API_ADDRESS');
            if (!$api) {
                throw new Exception('RCO_API_ADDRESS not set up: See documentation for setup under External Resources');
            }

            $client = new Client([
                // Base URI is used with relative requests
                'base_uri' => $api,
                // You can set any number of default request options.
                'timeout'  => 2.0,
            ]);
            $rco = Organization::find($id);
            try {
                set_time_limit(0);
                $queryParams = [
                    'where' => "name='".$rco->ORGANIZATION_NAME."'",
                    'geometryType' => 'esriGeometryPoint',
                    'inSR' => 4326,
                    'spatialRel' => 'esriSpatialRelWithin',
                    'resultType' => 'none',
                    'distance' => '0.0',
                    'units' => 'esriSRUnit_Meter',
                    'returnGeodetic' => 'false',
                    'outFields' => '*',
                    'returnGeometry' => 'true',
                    'returnCentroid' => 'false',
                    'multipatchOption' => 'xyFootprint',
                    'maxAllowableOffset' => '',
                    'geometryPrecision' => '',
                    'outSR' => 4326,
                    'returnIdsOnly' => 'false',
                    'returnCountOnly' => 'false',
                    'returnExtentOnly' => 'false',
                    'returnDistinctValues' => 'false',
                    'orderByFields' => '',
                    'groupByFieldsForStatistics' => '',
                    'outStatistics' => '',
                    'resultOffset' => '',
                    'resultRecordCount' => '',
                    'returnZ' => 'false',
                    'returnM' => 'false',
                    'quantizationParameters' => '',
                    'sqlFormat' => 'none',
                    'f' => 'pgeojson',
                ];
                /** @var ResponseInterface $response */
                $response = $client->get('0/query', [
                    'query' => $queryParams,
                    'connect_timeout' => 20
                ]);

                if ($response->getStatusCode() === 200) {
                    $json = json_decode($response->getBody()->getContents());
                    foreach ($json->features as $rco) {
                        $current = $rco->properties;
                        $current->geometry = $rco->geometry;
                        $current = $this->sanitizeRCO($current);
                        Cache::put("rco_".$current->id, $current, Carbon::today()->endOfDay());
                        $rco = $current;
                    }
                } else {
                    // We didn't get a success, handle exception
                    throw $this->generateException($response);
                }
            } catch (Exception $e) {
            // Something bad happened during the
            throw $e;
            }
        } else {
            $rco = Cache::get("rco_$id");
        }
        return $rco;
    }

    protected function sanitizeRCO($rco) {
        $myRCO = Organization::where('name', $rco->ORGANIZATION_NAME)->first();
        if (!$myRCO) {
            $myRCO = new Organization();
            $myRCO->name = $rco->ORGANIZATION_NAME;
            $myRCO->save();
        }

        $rco->id = $myRCO->id;
        $rco->social_media = $myRCO->socialMedia;
        $rco->committees = $myRCO->committees;
        return $rco;
    }

    /**
     * There are a couple ways that the external API can respond which we should package up as our own errors.
     * This method provides that wrapper, converting Guzzle and API errors into internal exceptions.
     *
     * @param ResponseInterface $response   The Guzzle Response object.
     * @param stdClass|null     $content    An optional JSON representation of the Response content.
     *
     * @return Exception    The Exception that should be thrown
     */
    protected function generateException(ResponseInterface $response): Exception
    {
        $errorResponse = sprintf(
            "Response code: %s, Message: %s",
            $response->getStatusCode(),
            $response->getReasonPhrase()
        );
        return new Exception($errorResponse);
    }
}<commit_msg>Swap attributes<commit_after><?php
namespace App\Services\RCO;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Collection;
use stdClass;

use App\Models\Organization;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
/**
 * This service interfaces with external API's that will allow it to convert a set of GPS coordinates
 * for a Philadelphia address into a set of Registered Community Organizations
 */
class GISRCOTranslationService implements RCOTranslationService
{
    /**
     * Given a specific point on a map, this method finds the Registered Community Organizations
     * that have that point within them by utilizing an external API
     *
     * @param stdClass $coordinates An object with 'latitude' and 'longitude' properties
     * @return Collection The set of Registered Community Organizations that a point belongs to
     * @throws Exception If the external service causes an issue
     */
    public function getRCOListForGPS(stdClass $coordinates): Collection
    {
        $rcos = [];
        $coordinatePair = sprintf('{"y":"%s","x":"%s"}', $coordinates->latitude, $coordinates->longitude);
        if (!Cache::has($coordinatePair)) {
            $api = env('RCO_API_ADDRESS');
            if (!$api) {
                throw new Exception('RCO_API_ADDRESS not set up: See documentation for setup under External Resources');
            }

            $client = new Client([
                // Base URI is used with relative requests
                'base_uri' => $api,
                // You can set any number of default request options.
                'timeout'  => 2.0,
            ]);

            try {
                set_time_limit(0);
                $queryParams = [
                    'where' => '1=1',
                    'geometry' => $coordinatePair,
                    'geometryType' => 'esriGeometryPoint',
                    'inSR' => 4326,
                    'spatialRel' => 'esriSpatialRelWithin',
                    'resultType' => 'none',
                    'distance' => '0.0',
                    'units' => 'esriSRUnit_Meter',
                    'returnGeodetic' => 'false',
                    'outFields' => '*',
                    'returnGeometry' => 'true',
                    'returnCentroid' => 'false',
                    'multipatchOption' => 'xyFootprint',
                    'maxAllowableOffset' => '',
                    'geometryPrecision' => '',
                    'outSR' => 4326,
                    'returnIdsOnly' => 'false',
                    'returnCountOnly' => 'false',
                    'returnExtentOnly' => 'false',
                    'returnDistinctValues' => 'false',
                    'orderByFields' => '',
                    'groupByFieldsForStatistics' => '',
                    'outStatistics' => '',
                    'resultOffset' => '',
                    'resultRecordCount' => '',
                    'returnZ' => 'false',
                    'returnM' => 'false',
                    'quantizationParameters' => '',
                    'sqlFormat' => 'none',
                    'f' => 'pgeojson',
                ];
                /** @var ResponseInterface $response */
                $response = $client->get('0/query', [
                    'query' => $queryParams,
                    'connect_timeout' => 20
                ]);

                if ($response->getStatusCode() === 200) {
                    $json = json_decode($response->getBody()->getContents());
                    foreach ($json->features as $rco) {
                        $current = $rco->properties;
                        $current->geometry = $rco->geometry;
                        $current = $this->sanitizeRCO($current);
                        Cache::put("rco_".$current->id, $current, Carbon::today()->endOfDay());
                        $rcos[] = $current;
                    }
                } else {
                    // We didn't get a success, handle exception
                    throw $this->generateException($response);
                }
                Cache::put($coordinatePair, $rcos, Carbon::today()->endOfDay());
            } catch (Exception $e) {
                // Something bad happened during the
                throw $e;
            }
        } else {
            $rcos = Cache::get($coordinatePair);
        }
        return collect($rcos);
    }

    /**
     * Given the local ID for an RCO, get any information that should be associated with it
     * @param  int    $id The ID of the object we're looking for
     * @return stdClass     The description of the RCO
     */
    public function getRCO(int $id): stdClass
    {
        $rco = null;

        if (!Cache::has("rco_$id")) {
            $api = env('RCO_API_ADDRESS');
            if (!$api) {
                throw new Exception('RCO_API_ADDRESS not set up: See documentation for setup under External Resources');
            }

            $client = new Client([
                // Base URI is used with relative requests
                'base_uri' => $api,
                // You can set any number of default request options.
                'timeout'  => 2.0,
            ]);
            $rco = Organization::get($id);
            try {
                set_time_limit(0);
                $queryParams = [
                    'where' => "ORGANIZATION_NAME='".$rco->name."'",
                    'geometryType' => 'esriGeometryPoint',
                    'inSR' => 4326,
                    'spatialRel' => 'esriSpatialRelWithin',
                    'resultType' => 'none',
                    'distance' => '0.0',
                    'units' => 'esriSRUnit_Meter',
                    'returnGeodetic' => 'false',
                    'outFields' => '*',
                    'returnGeometry' => 'true',
                    'returnCentroid' => 'false',
                    'multipatchOption' => 'xyFootprint',
                    'maxAllowableOffset' => '',
                    'geometryPrecision' => '',
                    'outSR' => 4326,
                    'returnIdsOnly' => 'false',
                    'returnCountOnly' => 'false',
                    'returnExtentOnly' => 'false',
                    'returnDistinctValues' => 'false',
                    'orderByFields' => '',
                    'groupByFieldsForStatistics' => '',
                    'outStatistics' => '',
                    'resultOffset' => '',
                    'resultRecordCount' => '',
                    'returnZ' => 'false',
                    'returnM' => 'false',
                    'quantizationParameters' => '',
                    'sqlFormat' => 'none',
                    'f' => 'pgeojson',
                ];
                /** @var ResponseInterface $response */
                $response = $client->get('0/query', [
                    'query' => $queryParams,
                    'connect_timeout' => 20
                ]);

                if ($response->getStatusCode() === 200) {
                    $json = json_decode($response->getBody()->getContents());
                    foreach ($json->features as $rco) {
                        $current = $rco->properties;
                        $current->geometry = $rco->geometry;
                        $current = $this->sanitizeRCO($current);
                        Cache::put("rco_".$current->id, $current, Carbon::today()->endOfDay());
                        $rco = $current;
                    }
                } else {
                    // We didn't get a success, handle exception
                    throw $this->generateException($response);
                }
            } catch (Exception $e) {
            // Something bad happened during the
            throw $e;
            }
        } else {
            $rco = Cache::get("rco_$id");
        }
        return $rco;
    }

    protected function sanitizeRCO($rco) {
        $myRCO = Organization::where('name', $rco->ORGANIZATION_NAME)->first();
        if (!$myRCO) {
            $myRCO = new Organization();
            $myRCO->name = $rco->ORGANIZATION_NAME;
            $myRCO->save();
        }

        $rco->id = $myRCO->id;
        $rco->social_media = $myRCO->socialMedia;
        $rco->committees = $myRCO->committees;
        return $rco;
    }

    /**
     * There are a couple ways that the external API can respond which we should package up as our own errors.
     * This method provides that wrapper, converting Guzzle and API errors into internal exceptions.
     *
     * @param ResponseInterface $response   The Guzzle Response object.
     * @param stdClass|null     $content    An optional JSON representation of the Response content.
     *
     * @return Exception    The Exception that should be thrown
     */
    protected function generateException(ResponseInterface $response): Exception
    {
        $errorResponse = sprintf(
            "Response code: %s, Message: %s",
            $response->getStatusCode(),
            $response->getReasonPhrase()
        );
        return new Exception($errorResponse);
    }
}
{'fraction_non_alphanumeric': 0.12311571561694896, 'fraction_numerical': 0.004385243655100586, 'mean_word_length': 1.436431623931624, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 219, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2644631', 'n_tokens_mistral': 4743, 'n_tokens_neox': 4425, 'n_words': 1117}