PHP code example of moesif / moesif-symfony

1. Go to this page and download the library: Download moesif/moesif-symfony library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moesif / moesif-symfony example snippets


use Moesif\MoesifBundle\Interfaces\MoesifHooksInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MyMoesifHooks implements MoesifHooksInterface {
   // your implementation of every method follows.
}


use Moesif\MoesifBundle\Service\MoesifApiService;

private $moesifApiService;

public function __construct(MoesifApiService $moesifApiService)
{
    $this->moesifApiService = $moesifApiService;
}

// metadata can be any custom object
$data->metadata = array(
    "email" => "[email protected]",
    "first_name" => "John",
    "last_name" => "Doe",
    "title" => "Software Engineer",
    "sales_info" => array(
        "stage" => "Customer",
        "lifetime_value" => 24000,
        "account_owner" => "[email protected]"
    )
);

$userData = [
    'user_id' => $data['userId'],
    'user_email' => $data['userEmail'],
    'company_id' => $data['companyId'],
    'metadata' => $data['metadata'] ?? [], // Include metadata if provided
    // Add more fields as needed
];

$this->moesifApiService->updateUser($userData);


use Moesif\MoesifBundle\Service\MoesifApiService;

private $moesifApiService;

public function __construct(MoesifApiService $moesifApiService)
{
    $this->moesifApiService = $moesifApiService;
}

// metadata can be any custom object
$data->metadata = array(
    "email" => "[email protected]",
    "first_name" => "John",
    "last_name" => "Doe",
    "title" => "Software Engineer",
    "sales_info" => array(
        "stage" => "Customer",
        "lifetime_value" => 24000,
        "account_owner" => "[email protected]"
    )
);

$userDataA = [
    'user_id' => $data['userIdA'],
    'user_email' => $data['userEmailA'],
    'company_id' => $data['companyIdA'],
    'metadata' => $data['metadata'] ?? [], // Include metadata if provided
    // Add more fields as needed
];

$userDataB = [
    'user_id' => $data['userIdB'],
    'user_email' => $data['userEmailB'],
    'company_id' => $data['companyIdB'],
    'metadata' => $data['metadata'] ?? [], // Include metadata if provided
    // Add more fields as needed
];

$users = array($userDataA, $userDataB)
$this->moesifApiService->updateUsersBatch($user);


use Moesif\MoesifBundle\Service\MoesifApiService;

private $moesifApiService;

public function __construct(MoesifApiService $moesifApiService)
{
    $this->moesifApiService = $moesifApiService;
}

// metadata can be any custom object
$data->metadata = array(
    "org_name" => "Acme, Inc",
    "plan_name" => "Free",
    "deal_stage" => "Lead",
    "mrr" => 24000,
    "demographics" => array(
        "alexa_ranking" => 500000,
        "employee_count" => 47
    )
);

// Prepare company data for Moesif
$companyData = [
    'company_id' => $data['companyId'],
    'company_domain' => $data['companyDomain'],
    'metadata' => $data['metadata'] ?? [], // Include metadata if provided
    // Add more fields as needed
];

$this->moesifApiService->updateCompany($companyData);


use Moesif\MoesifBundle\Service\MoesifApiService;

private $moesifApiService;

public function __construct(MoesifApiService $moesifApiService)
{
    $this->moesifApiService = $moesifApiService;
}

// metadata can be any custom object
$data->metadata = array(
    "org_name" => "Acme, Inc",
    "plan_name" => "Free",
    "deal_stage" => "Lead",
    "mrr" => 24000,
    "demographics" => array(
        "alexa_ranking" => 500000,
        "employee_count" => 47
    )
);

// Prepare company data for Moesif
$companyDataA = [
    'company_id' => $data['companyIdA'],
    'company_domain' => $data['companyDomainA'],
    'metadata' => $data['metadata'] ?? [], // Include metadata if provided
    // Add more fields as needed
];

$companyDataB = [
    'company_id' => $data['companyIdB'],
    'company_domain' => $data['companyDomainB'],
    'metadata' => $data['metadata'] ?? [], // Include metadata if provided
    // Add more fields as needed
];

$companies = array($companyDataA, $companyDataB)

$this->moesifApiService->updateCompaniesBatch($companies);
 php


namespace App\Configuration;

use Moesif\MoesifBundle\Interfaces\MoesifHooksInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class MyMoesifHooks implements MoesifHooksInterface {

  public function __construct() {
  }

  public function identifyUserId(Request $request, Response $response): string|null
  {
    return 'nihao1';
  }

  public function identifyCompanyId(Request $request, Response $response): string|null
  {
    return $request->headers->get('X-Company-Id');
  }

  public function identifySessionToken(Request $request, Response $response): string|null
  {
    return null;
  }

  public function getMetadata(Request $request, Response $response): ?array
  {
      return null;
  }

  public function skip(Request $request, Response $response): bool
  {
      return false;
  }

  public function maskRequestHeaders(array $headers): array
  {
      return $headers;
  }

  public function maskResponseHeaders(array $headers): array
  {
      return $headers;
  }

  public function maskRequestBody($body)
  {
      // this can be a string or array object.
      // because prior to php 8, can not declare union type (such as string|array)
      return $body;
  }

  public function maskResponseBody($body)
  {
      // this can be a string or array object.
      // because prior to php 8, can not declare union type (such as string|array)
      return $body;
  }
}