<commit_before><?php

namespace DoSomething\Gateway;

use DoSomething\Gateway\Common\RestApiClient;
use DoSomething\Gateway\Resources\NorthstarClient;
use DoSomething\Gateway\Resources\NorthstarUser;
use DoSomething\Gateway\Resources\NorthstarUserCollection;
use DoSomething\Gateway\Resources\NorthstarClientCollection;

class Northstar extends RestApiClient
{
    use AuthorizesWithNorthstar;

    /**
     * Create a new Northstar API client.
     * @param array $config
     * @param array $overrides
     */
    public function __construct($config = [], $overrides = [])
    {
        $base_url = $config['url'];

        // Set required fields for OAuth authentication trait.
        $this->authorizationServerUri = $config['url'];
        $this->grant = $config['grant'];
        $this->config = $config;

        if (! empty($config['bridge'])) {
            $this->bridge = $config['bridge'];
        }

        parent::__construct($base_url, $overrides);
    }

    /**
     * Send a GET request to return all users matching a given
     * query from Northstar.
     *
     * @param array $inputs - Filter, search, or pagination queries
     * @return NorthstarUserCollection
     */
    public function getAllUsers($inputs = [])
    {
        $response = $this->get('v1/users', $inputs);

        return new NorthstarUserCollection($response);
    }

    /**
     * Send a GET request to return a user with that id.
     *
     * @param string $type - 'id', 'email', 'mobile'
     * @param string $id - ID, email, id, phone
     * @return NorthstarUser
     */
    public function getUser($type, $id)
    {
        $response = $this->get('v1/users/'.$type.'/'.$id);

        if (is_null($response)) {
            return null;
        }

        return new NorthstarUser($response['data']);
    }

    /**
     * Send a POST request to create/update a user in Northstar.
     * Requires an `admin` scoped API key.
     *
     * @param array $input - Fields to update in profile
     * @return NorthstarUser|null
     */
    public function createUser($input)
    {
        $response = $this->post('v1/users', $input);

        return new NorthstarUser($response['data']);
    }

    /**
     * Send a PUT request to update a user in Northstar.
     *
     * @param string $id - Northstar User ID
     * @param array $input - Fields to update in profile
     * @return mixed
     */
    public function updateUser($id, $input)
    {
        $response = $this->put('v1/users/_id/'.$id, $input);

        return new NorthstarUser($response['data']);
    }

    /**
     * Send a DELETE request to delete a user from Northstar.
     * Requires an `admin` scoped API key.
     *
     * @param $id - Northstar user ID
     * @return bool - Whether user was successfully deleted
     */
    public function deleteUser($id)
    {
        $success = $this->delete('v1/users/_id/'.$id);

        return $success;
    }

    /**
     * Send a GET request to return all Northstar keys.
     * Requires an `admin` scoped API key.
     *
     * @param array $inputs - Pagination queries<commit_msg>adding mergeUsers method to merge user accounts via northstar API<commit_after><?php

namespace DoSomething\Gateway;

use DoSomething\Gateway\Common\RestApiClient;
use DoSomething\Gateway\Resources\NorthstarClient;
use DoSomething\Gateway\Resources\NorthstarUser;
use DoSomething\Gateway\Resources\NorthstarUserCollection;
use DoSomething\Gateway\Resources\NorthstarClientCollection;

class Northstar extends RestApiClient
{
    use AuthorizesWithNorthstar;

    /**
     * Create a new Northstar API client.
     * @param array $config
     * @param array $overrides
     */
    public function __construct($config = [], $overrides = [])
    {
        $base_url = $config['url'];

        // Set required fields for OAuth authentication trait.
        $this->authorizationServerUri = $config['url'];
        $this->grant = $config['grant'];
        $this->config = $config;

        if (! empty($config['bridge'])) {
            $this->bridge = $config['bridge'];
        }

        parent::__construct($base_url, $overrides);
    }

    /**
     * Send a GET request to return all users matching a given
     * query from Northstar.
     *
     * @param array $inputs - Filter, search, or pagination queries
     * @return NorthstarUserCollection
     */
    public function getAllUsers($inputs = [])
    {
        $response = $this->get('v1/users', $inputs);

        return new NorthstarUserCollection($response);
    }

    /**
     * Send a GET request to return a user with that id.
     *
     * @param string $type - 'id', 'email', 'mobile'
     * @param string $id - ID, email, id, phone
     * @return NorthstarUser
     */
    public function getUser($type, $id)
    {
        $response = $this->get('v1/users/'.$type.'/'.$id);

        if (is_null($response)) {
            return null;
        }

        return new NorthstarUser($response['data']);
    }

    /**
     * Send a POST request to create/update a user in Northstar.
     * Requires an `admin` scoped API key.
     *
     * @param array $input - Fields to update in profile
     * @return NorthstarUser|null
     */
    public function createUser($input)
    {
        $response = $this->post('v1/users', $input);

        return new NorthstarUser($response['data']);
    }

    /**
     * Send a PUT request to update a user in Northstar.
     *
     * @param string $id - Northstar User ID
     * @param array $input - Fields to update in profile
     * @return mixed
     */
    public function updateUser($id, $input)
    {
        $response = $this->put('v1/users/_id/'.$id, $input);

        return new NorthstarUser($response['data']);
    }

    /**
     * Send a DELETE request to delete a user from Northstar.
     * Requires an `admin` scoped API key.
     *
     * @param $id - Northstar user ID
     * @return bool - Whether user was successfully deleted
     */
    public function deleteUser($id)
    {
        $success = $this->delete('v1/users/_id/'.$id);

        return $success;
    }

    /**
     * Send a POST request to merge two users in Northstar.
     *
     * @param string $id - Northstar User ID.
     * @param string $duplicateId - Northstar User ID of duplicate user.
     * @param bool $pretend - Whether to persist the merge or not.
     * @return mixed
     */
    public function mergeUsers($id, $duplicateId, $pretend = false)
    {
        $response = $this->post('v1/users/'.$id.'/merge/?pretend='.$pretend, ['id' => $duplicateId]);

        return new NorthstarUser($response['data']);
    }

    /**
     * Send a GET request to return all Northstar keys.
     * Requires an `admin` scoped API key.
     *
     * @param array $inputs - Pagination queries
{'fraction_non_alphanumeric': 0.12535107169253512, 'fraction_numerical': 0.0016260162601626016, 'mean_word_length': 2.3931795386158474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4847383', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1944, 'n_words': 566}